Struts2学习笔记----访问Servlet API

来源:互联网 发布:淘宝隐形眼镜 编辑:程序博客网 时间:2024/04/27 15:03

在struts2框架中,Action与Servlet Api相互分离,这种底耦合性给开发带来了便利。但是很多时候,Action不访问Servlet API,将不能够实现业务逻辑处理。例如,得到Session和Application等。

struts2为我们提供了两种途径来访问ServletAPI,一种是间接访问;还有一种是直接访问。其中直接访问有区分为IOC和非IOC两种方式。下面我们来一一介绍:

1:间接访问(ActionContext)

           Action运行期间所用到的数据都保存在ActionContext中,例如session会话,客户端提交的参数等信息。所以ActionContext是Action的一个上下文对象。

语法:ActionContext actionContext = ActionContext.getContext();

 

例子:

addBook.java:

package action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AddBook extends ActionSupport {
    private String bookName;
    private double bookPrice;
    private String bookPress;

    //省去get和set方法
    @Override
    public String execute() throws Exception {
        ActionContext actionContext = ActionContext.getContext();
        actionContext.getSession().put("bookName", this.bookName);
        actionContext.getSession().put("bookPrice", this.bookPrice);
        actionContext.getSession().put("bookPress", this.bookPress);
        return SUCCESS;
    }
}

 

------------------------------------------------------------------------------------------------------------------

addBook.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <center>
        <s:form action="addBook" method="post">
            <s:textfield name="bookName" label="书名"></s:textfield>
            <s:textfield name="bookPrice" label="价格"></s:textfield>
            <s:textfield name="bookPress" label="出版社"></s:textfield>
            <s:submit value="提交 " />
        </s:form>
    </center>
</body>
</html>

------------------------------------------------------------------------------------------------------------------

addSuccess.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>&nbsp;
读取session中的内容,您添加的图书信息为:<br />
    书名:<s:property value="#session.bookName" /><br />
    价格:<s:property value="#session.bookPrice" /><br />
    出版社:<s:property value="#session.bookPress" /><br />
</body>
</html>

------------------------------------------------------------------------------------------------------------------

2.直接访问

   1>.IOC方式

在struts2框架中,通过IOC方式访问ServletAPI,就必须实现Action中相应的接口,这些接口是:

org.apache.struts2.util.ServletContextAware

org.apache.struts2.interceptor.ServletRequestAware

org.apache.struts2.interceptor.ServletResponseAware

org.apache.struts2.interceptor.ServletSessionAware

 

你可以实现其中一个接口,也可以全部都实现,根据具体情况而定。

 

例子:

IocAction.java:

package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class IocAction extends ActionSupport implements ServletRequestAware {
    private String bookName;
    private double bookPrice;
    private String bookPress;
    private HttpServletRequest request;
     //省去get和set方法
    public void setServletRequest(HttpServletRequest arg0) {
        this.request=arg0;
    }
    @Override
    public String execute() throws Exception {
        HttpSession session = request.getSession();
        session.setAttribute("bookName", this.bookName);
        session.setAttribute("bookPrice", this.bookPrice);
        session.setAttribute("bookPress", this.bookPress);
        return SUCCESS;
    }
}

                  -----------------------------------------------------------------------------------------------------------------

    2>.非IOC方式(ServletActionContext)

在struts2中,提供了ServletActionContext类来获得ServletAPI,ServletActionContext类为我们提供了静态方法,直接调用即可:

例子:

NoloCAction.java

package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class NoloCAction extends ActionSupport {
    private String bookName;
    private double bookPrice;
    private String bookPress;
//省去get和set方法
    @Override
    public String execute() throws Exception {
        HttpServletRequest request=ServletActionContext.getRequest();
        HttpSession session=request.getSession();
        session.setAttribute("bookName", this.bookName);
        session.setAttribute("bookPrice", this.bookPrice);
        session.setAttribute("bookPress", this.bookPress);
        return SUCCESS;
    }
}

 

总结:Action对Servlet API的访问,分别为间接访问和直接访问(IOC和非IOC方式)。对于间接访问,只能得到request和session对象(Map类型的),不能得到response对象。对于IOC方式,使用起来比较麻烦,并且与Servlet API耦合大,在这里不推荐使用。对于非IOC方式,推荐使用,代码清晰而且又能满足要求。