在传统的Web开发中,经常会用到Servlet API中的HttpServletRequest、HttpSession和ServletContext。Struts 2框架让我们可以直接访问和设置action及模型对象的数据,这降低了对HttpServletRequest对象的使用需求,但在某些应用中,我们可能会需要在action中去访问HttpServletRequest对象以及其他两种对象,例如,用户登录成功后,我们应该将用户信息保存到Session中。
Struts 2提供了多种方式来访问上述的三种对象,归结起来,可以划分为两大类:与Servlet API解耦的访问方式和与Servlet API耦合的访问方式.
与Servlet API解耦的访问方式
为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对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对象。
通过ActionContext来获取request、session和application对象的LoginAction:
- import java.util.Map;
- import com.opensymphony.xwork2.ActionContext;
- 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;
- }
- @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";
- }
- }
在成功页面中,可以使用JSP内置的表达式语言来访问request、session和application范围的数据:
- <%@ page contentType="text/html;charset=GBK" %>
- <html>
- <head><title>欢迎页面</title></head>
- <body>
- <h3>${sessionScope.username},${requestScope.greeting}。<br>
- 本站的访问量是:${applicationScope.counter}</h3>
- </body>
- </html>
除了利用ActionContext来获取request、session和application对象这种方式外,Action类还可以实现某些特定的接口,让Struts 2框架在运行时向Action实例注入request、session和application对象。与之对应的三个接口和它们的方法如下所示:
? org.apache.struts2.interceptor.RequestAware
框架利用该接口,向Action实例注入request Map对象。该接口只有一个方法,如下:
— public void setRequest(Map request)
? org.apache.struts2.interceptor.SessionAware
框架利用该接口,向Action实例注入session Map对象。该接口只有一个方法,如下:
— void setSession(Map session)
? org.apache.struts2.interceptor.ApplicationAware
框架利用该接口,向Action实例注入application Map对象。该接口只有一个方法,如下:
— void setApplication(Map application)
通过接口注入来获取request、session和application对象的LoginAction:
- import java.util.Map;
- import org.apache.struts2.interceptor.ApplicationAware;
- import org.apache.struts2.interceptor.RequestAware;
- import org.apache.struts2.interceptor.SessionAware;
- @SuppressWarnings("unchecked")
- public class LoginAction2 implements RequestAware, SessionAware,
- ApplicationAware {
- private Map request;
- private Map session;
- private Map application;
- private String name;
- private String password;
- public void setRequest(Map request) {
- this.request = request;
- }
- public void setSession(Map session) {
- this.session = session;
- }
- public void setApplication(Map application) {
- this.application = application;
- }
- 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 {
- // 在请求中放置欢迎信息。
- request.put("greeting", "欢迎您来到程序员之家");
- // 在session中保存user对象
- session.put("password", password);
- // 统计用户访问量,在application中保存用户访问量数据
- Integer count = (Integer) application.get("counter");
- if (null == count)
- count = 1;
- else
- count++;
- application.put("counter", count);
- return "success";
- }
- }
LoginAction类实现了RequestAware、SessionAware和ApplicationAware接口,框架在运行时会调用这三个接口中的方法,向LoginAction2注入request、session和application对象。在execute()方法中不再需要访问ActionContext,因此我们删除了与之相关的代码。
与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:
- 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";
- }
- }
除了利用ServletActionContext来获取HttpServletRequest对象和ServletContext对象这种方式外,Action类还可以实现ServletRequestAware和ServletContextAware接口,由Struts 2框架向Action实例注入HttpServletRequest和ServletContext对象。
org.apache.struts2.interceptor.ServletRequestAware接口只有一个方法,如下所示:
? void setServletRequest(HttpServletRequest request)
org.apache.struts2.util.ServletContextAware接口也只有一个方法,如下所示:
? void setServletContext(ServletContext context)
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import org.apache.struts2.interceptor.ServletRequestAware;
- import org.apache.struts2.util.ServletContextAware;
- public class LoginAction implements ServletRequestAware, ServletContextAware {
- private HttpServletRequest request;
- private ServletContext context;
- 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 {
- HttpSession session = request.getSession();
- // 在请求中放置欢迎信息。
- 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";
- }
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- }
- public void setServletContext(ServletContext context) {
- this.context = context;
- }
- }
LoginAction类实现了ServletRequestAware和ServletContextAware接口,框架在运行时会调用这两个接口中的方法,向LoginAction4注入HttpServletRequest和ServletContext对象。在execute()方法中不再需要访问ServletActionContext,因此我们删除了与之相关的代码。