间接访问Servlet API

通过ActionContext类来访问Servlet的对象,只能获得request

    public String checkLogin(){
        ActionContext ac=ActionContext.getContext();
        ac.getSession().put("login", name);
        ac.getApplication().put("login", this.name);
        return SUCCESS;
    }

直接访问Servlet API

Ioc(控制反转)方式,通过实现ServletRequestAware接口来获得request对象,不推荐使用,因为实现起来麻烦

public class IocAction extends ActionSupport implements ServletRequestAware{
    private String name;
    private String pwd;
    private HttpServletRequest request;
    @Override
    public void setServletRequest(HttpServletRequest arg0) {
        // TODO Auto-generated method stub
        this.request=request;
    }
    /* 省略set()和get()方法*/
    public String checkLogin(){
        HttpSession session=request.getSession();
        session.setAttribute("login", this.name);
        return SUCCESS;
    }
}

非Ioc方式

public class IocAction extends ActionSupport{
    private String name;
    private String pwd;
    /* 省略set()和get()方法*/
    public String checkLogin(){
        HttpServletRequest request=ServletActionContext.getRequest();
        HttpSession session=request.getSession();
        session.setAttribute("login", this.name);
        return SUCCESS;
    }
}

配置Action

Action属性

name:客户端发送请求的地址映射名称,即jsp中的form的action名称
class:进行业务逻辑处理的Action名称,即src中对应的java类
method:进行业务逻辑处理的Action中的方法名称,默认为execute()
result:Action返回不同string后其对应的视图页面路径

<action name="LoginAction" class="ch3.LoginAction" method="checkLogin">
    <result name="success">/index.jsp</result>
    <result name="login">/login.jsp</result>
</action>

当一个result被多次使用时,可以把该结果视图配置为全局结果。
当局部结果与全局结果重名时,局部结果覆盖全局结果。

<global-results>
    <result name="success">/index.jsp</result>
</global-results>

动态指定Action方法

<s:form action="LoginAction!checkLogin" namespace="/login">

<action name="LoginAction" class="ch3.LoginAction">

等价于

<s:form action="LoginAction" namespace="/login">

<action name="LoginAction" class="ch3.LoginAction" method="checkLogin">

使用通配符配置Action

<action name="*_*" class="ch3.{1}" method="{2}">
    <result name="success">/index.jsp</result>
    <result name="login">/{1}.jsp</result>
</action>
假设*_*为Login_checkLogin
则{1}对应Login,{2}对应checkLogin

类型转换

Struts2提供了丰富的类型转换

简单类型转换

addBook.jsp

<s:form action="addBookAction" namespace="/Book" method="addBook">
    <s:textfield name="name" label="图书名称"></s:textfield>
    <s:textfield name="price" label="图书价格"></s:textfield>
    <s:textfield name="num" label="发行数量"></s:textfield>
    <s:textfield name="date" label="发行日期"></s:textfield>
    <s:submit value="添加" />
</s:form>

addBookAction.java

package ch3;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class addBookAction extends ActionSupport{
    private String name;
    private double price;
    private int num;
    private Date date;
    /* 省略set()和get()方法*/
    public String addBook(){
        return SUCCESS;
    }
}

Struts.xml

<package name="Book" extends="struts-default">
    <action name="*Action" class="ch3.{1}Action" method="{1}">
        <result name="success">/addSuccess.jsp</result>
    </action>
</package>

addSuccess.jsp

添加成功!<br>
您添加的信息如下:<br>
书名:<s:property value="name" /><br>
价格:<s:property value="price" /><br>
数量:<s:property value="num" /><br>
日期:<s:property value="date" /><br>

addBook.jspaddSuccess.jsp
也可以定义一个Book.java

    private String name;
    private double price;
    private int num;
    private Date date;
    /* 省略set()和get()方法*/

addBookAction改为:

public class addBookAction extends ActionSupport{
    private Book b;
    public Book getB() {
        return b;
    }
    public void setB(Book b) {
        this.b = b;
    }   
    public String addBook(){
        return SUCCESS;
    }
}

再将页面中的name、price、num、date改为b.name、b.price、b.num、b.date,结果与之前相同。

集合类型转换

addBookAction.java

package ch3;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class addBookAction extends ActionSupport{
    private List<Book>bookList=new ArrayList<Book>();

    public List<Book> getBookList() {
        return bookList;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }
    public String addBook(){
        System.out.println(bookList.size());
        System.out.println(bookList.get(0).getName());
        return INPUT;
    }
}

addBook.jsp
不会写循环。。。。

<s:form action="addBookAction" namespace="/Book" method="addBook">
    <s:textfield name="bookList[0].name" label="图书名称"></s:textfield>
    <s:textfield name="bookList[0].price" label="图书价格"></s:textfield>
    <s:textfield name="bookList[0].num" label="发行数量"></s:textfield>
    <s:textfield name="bookList[1].name" label="图书名称"></s:textfield>
    <s:textfield name="bookList[1].price" label="图书价格"></s:textfield>
    <s:textfield name="bookList[1].num" label="发行数量"></s:textfield>
    <s:textfield name="bookList[2].name" label="图书名称"></s:textfield>
    <s:textfield name="bookList[2].price" label="图书价格"></s:textfield>
    <s:textfield name="bookList[2].num" label="发行数量"></s:textfield>
    <s:textfield name="bookList[3].name" label="图书名称"></s:textfield>
    <s:textfield name="bookList[3].price" label="图书价格"></s:textfield>
    <s:textfield name="bookList[3].num" label="发行数量"></s:textfield>
    <s:submit value="添加" />
</s:form>
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐