Struts2中获取request,session,application对象

在传统的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:

Java代码   收藏代码
  1. import java.util.Map;  
  2. import com.opensymphony.xwork2.ActionContext;  
  3.   
  4. public class LoginAction {  
  5.     private String name;  
  6.     private String password;  
  7.   
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.     public String getPassword() {  
  15.         return password;  
  16.     }  
  17.     public void setPassword(String password) {  
  18.         this.password = password;  
  19.     }  
  20.   
  21.     @SuppressWarnings("unchecked")  
  22.     public String execute() {  
  23.         ActionContext context = ActionContext.getContext();  
  24.         Map request = (Map) context.get("request");  
  25.         Map session = context.getSession();  
  26.         Map application = context.getApplication();  
  27.         // 在请求中放置欢迎信息。  
  28.         request.put("greeting""欢迎您来到程序员之家");  
  29.         // 在session中保存password属性  
  30.         session.put("password", password);  
  31.         // 统计用户访问量,在application中保存用户访问量数据  
  32.         Integer count = (Integer) application.get("counter");  
  33.         if (null == count)  
  34.             count = 1;  
  35.         else  
  36.             count++;  
  37.         application.put("counter", count);  
  38.         return "success";  
  39.     }  
  40. }  

在成功页面中,可以使用JSP内置的表达式语言来访问request、session和application范围的数据:

Js代码   收藏代码
  1. <%@ page contentType="text/html;charset=GBK" %>  
  2.   
  3. <html>  
  4.   
  5.     <head><title>欢迎页面</title></head>  
  6.   
  7.     <body>  
  8.   
  9.         <h3>${sessionScope.username},${requestScope.greeting}。<br>  
  10.   
  11.         本站的访问量是:${applicationScope.counter}</h3>  
  12.   
  13.     </body>  
  14.   
  15. </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:

Java代码   收藏代码
  1. import java.util.Map;  
  2. import org.apache.struts2.interceptor.ApplicationAware;  
  3. import org.apache.struts2.interceptor.RequestAware;  
  4. import org.apache.struts2.interceptor.SessionAware;  
  5.   
  6. @SuppressWarnings("unchecked")  
  7. public class LoginAction2 implements RequestAware, SessionAware,  
  8.         ApplicationAware {  
  9.   
  10.     private Map request;  
  11.     private Map session;  
  12.     private Map application;  
  13.     private String name;  
  14.     private String password;  
  15.       
  16.     public void setRequest(Map request) {  
  17.         this.request = request;  
  18.     }  
  19.     public void setSession(Map session) {  
  20.         this.session = session;  
  21.     }  
  22.     public void setApplication(Map application) {  
  23.         this.application = application;  
  24.     }  
  25.     public String getName() {  
  26.         return name;  
  27.     }  
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.     public String getPassword() {  
  32.         return password;  
  33.     }  
  34.     public void setPassword(String password) {  
  35.         this.password = password;  
  36.     }  
  37.       
  38.     public String execute() throws Exception {  
  39.         // 在请求中放置欢迎信息。  
  40.         request.put("greeting""欢迎您来到程序员之家");  
  41.         // 在session中保存user对象  
  42.         session.put("password", password);  
  43.         // 统计用户访问量,在application中保存用户访问量数据  
  44.         Integer count = (Integer) application.get("counter");  
  45.         if (null == count)  
  46.             count = 1;  
  47.         else  
  48.             count++;  
  49.         application.put("counter", count);  
  50.         return "success";  
  51.     }  
  52. }  

 

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:

Java代码   收藏代码
  1. import javax.servlet.ServletContext;  
  2. import javax.servlet.http.HttpServletRequest;  
  3. import javax.servlet.http.HttpSession;  
  4. import org.apache.struts2.ServletActionContext;  
  5. import com.opensymphony.xwork2.Action;  
  6.   
  7. public class LoginAction {  
  8.       
  9.     private String name;  
  10.     private String password;  
  11.       
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     public String getPassword() {  
  19.         return password;  
  20.     }  
  21.     public void setPassword(String password) {  
  22.         this.password = password;  
  23.     }  
  24.   
  25.     public String execute() throws Exception {  
  26.         HttpServletRequest request = ServletActionContext.getRequest();  
  27.         HttpSession session = request.getSession();  
  28.         ServletContext context = ServletActionContext.getServletContext();  
  29.   
  30.         // 在请求中放置欢迎信息。  
  31.         request.setAttribute("greeting""欢迎您来到程序员之家");  
  32.         // 在session中保存user对象  
  33.         session.setAttribute("password", password);  
  34.         // 统计用户访问量,在application中保存用户访问量数据  
  35.         Integer count = (Integer) context.getAttribute("counter");  
  36.         if (null == count)  
  37.             count = 1;  
  38.         else  
  39.             count++;  
  40.         context.setAttribute("counter", count);  
  41.         return "success";  
  42.     }  
  43. }  

 

除了利用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)

 

 

Java代码   收藏代码
  1. import javax.servlet.ServletContext;  
  2. import javax.servlet.http.HttpServletRequest;  
  3. import javax.servlet.http.HttpSession;  
  4. import org.apache.struts2.interceptor.ServletRequestAware;  
  5. import org.apache.struts2.util.ServletContextAware;  
  6.   
  7. public class LoginAction implements ServletRequestAware, ServletContextAware {  
  8.   
  9.     private HttpServletRequest request;  
  10.     private ServletContext context;  
  11.     private String name;  
  12.     private String password;  
  13.   
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public String getPassword() {  
  21.         return password;  
  22.     }  
  23.     public void setPassword(String password) {  
  24.         this.password = password;  
  25.     }  
  26.   
  27.     public String execute() throws Exception {  
  28.         HttpSession session = request.getSession();  
  29.         // 在请求中放置欢迎信息。  
  30.         request.setAttribute("greeting""欢迎您来到程序员之家");  
  31.         // 在session中保存user对象  
  32.         session.setAttribute("password", password);  
  33.         // 统计用户访问量,在application中保存用户访问量数据  
  34.         Integer count = (Integer) context.getAttribute("counter");  
  35.         if (null == count)  
  36.             count = 1;  
  37.         else  
  38.             count++;  
  39.         context.setAttribute("counter", count);  
  40.         return "success";  
  41.     }  
  42.   
  43.     public void setServletRequest(HttpServletRequest request) {  
  44.         this.request = request;  
  45.     }  
  46.   
  47.     public void setServletContext(ServletContext context) {  
  48.         this.context = context;  
  49.     }  
  50. }  

 

LoginAction类实现了ServletRequestAware和ServletContextAware接口,框架在运行时会调用这两个接口中的方法,向LoginAction4注入HttpServletRequest和ServletContext对象。在execute()方法中不再需要访问ServletActionContext,因此我们删除了与之相关的代码。

猜你喜欢

转载自blog.csdn.net/qq_38941937/article/details/79671055