在传统的Web开发中,常常会用到Servlet API中的HttpServletRequest、HttpSession和ServletContext。Struts 2框架让咱们能够直接访问和设置action及模型对象的数据,这下降了对HttpServletRequest对象的使用需求,但在某些应用中,咱们可能会须要在action中去访问HttpServletRequest对象以及其余两种对象,例如,用户登陆成功后,咱们应该将用户信息保存到Session中。Struts 2提供了多种方式来访问上述的三种对象,归结起来,能够划分为两大类:与Servlet API解耦的访问方式和与Servlet API耦合的访问方式.java
方式1、与Servlet API解耦的访问方式
为了不与Servlet API耦合在一块儿,方便Action类作单元测试,Struts HttpServletRequest、HttpSession和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest、HttpSession和ServletContext对应的Map对象来保存和读取数据。
要获取这三个Map对象,可使用com.opensymphony.xwork2.ActionContext类。
ActionContext是action执行的上下文,在ActionContext中保存了action执行所需的一组对象,包括parameters、request、session、application和locale等。ActionContext类定义了以下方法,用于获取HttpServletRequest、HttpSession和ServletContext对应的Map对象。
? public Object get(Object key)ActionContext类没有提供相似getRequest()这样的方法来获取封装了HttpServletRequest的Map对象。要获得请求Map对象,你须要为get()方法传递参数“request”。
? public Map getSession()
获取封装了HttpSession的Map对象。
? public Map getApplication()
获取封装了ServletContext的Map对象。程序员
private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @SuppressWarnings("unchecked") public String execute() { ActionContext context = ActionContext.getContext(); Map request = (Map) context.get("request"); Map session = context.getSession(); Map application = context.getApplication(); // 在请求中放置欢迎信息。 request.put("greeting", "欢迎您来到程序员之家"); // 在session中保存password属性 session.put("password", password); // 统计用户访问量,在application中保存用户访问量数据 Integer count = (Integer) application.get("counter"); if (null == count) count = 1; else count++; application.put("counter", count); return "success"; }
方式二。与Servlet API耦合的访问方式
直接访问Servlet API将使你的Action与Servlet环境耦合在一块儿,咱们知道对于HttpServletRequest、HttpServletResponse和ServletContext这些对象,它们都是由Servlet容器来构造的,与这些对象绑定在一块儿,测试时就须要有Servlet容器,不便于Action的单元测试。但有时候,咱们又确实须要直接访问这些对象,那么固然是以完成任务需求为主。
要直接获取HttpServletRequest和ServletContext对象,可使用org.apache.struts2. ServletActionContext类,该类是ActionContext的子类,在这个类中定义下面两个静态方法:
? public static HttpServletRequest getRequest()
获得HttpServletRequest对象。
? public static ServletContext getServletContext()
获得ServletContext对象。
此外,ServletActionContext类还给出了获取HttpServletResponse对象的方法,以下:
? public static HttpServletResponse getResponse()
注意:ServletActionContext类并无给出直接获得HttpSession对象的方法,HttpSession对象能够经过HttpServletRequest对象来获得。
除了上述的方法调用获得HttpServletRequest和ServletContext对象外,还能够调用ActionContext对象的get()方法,传递ServletActionContext.HTTP_REQUEST和ServletActionContext.SERVLET_CONTEXT键值来获得HttpServletRequest和ServletContext对象,以下所示:
? ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
获得与ServletActionContext.HTTP_REQUEST键值绑定的HttpServletRequest对象。
? ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);
获得与ServletActionContext.SERVLET_CONTEXT键值绑定的ServletContext对象。
一样的,也能够向ActionContext的get()方法传递ServletActionContext.HTTP_ RESPONSE键值来获得HttpServletResponse对象,以下:
? ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
建议读者采用第一种方式来获取HttpServletRequest和ServletContext对象,这样简单而又清晰。
经过ServletActionContext来获取HttpServletRequest和ServletContext对象的LoginAction:apache
import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class LoginAction { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); ServletContext context = ServletActionContext.getServletContext(); // 在请求中放置欢迎信息。 request.setAttribute("greeting", "欢迎您来到程序员之家"); // 在session中保存user对象 session.setAttribute("password", password); // 统计用户访问量,在application中保存用户访问量数据 Integer count = (Integer) context.getAttribute("counter"); if (null == count) count = 1; else count++; context.setAttribute("counter", count); return "success"; } }