zoukankan      html  css  js  c++  java
  • [Struts2]访问request,session和application对象(转)

    与Servlet API解耦的访问方式

    Structs2对HttpServletRequest,HttpSession,和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest,Httpsession,ServletContext对应的Map对象来保存和读取数据。

    要获取这三个Map对象,可以使用com.opensymphony.xwork2.ActionContext类。

    ActionContext是action执行的上下文,在ActionContext中保存了action执行所需要的一组对象,包括parameters,request,session,application,locale等。

    ActionContext类没有提供类似getRequest()这样的方法来获取封装了HttpServletRequest的Map对象。要请求Map对象,需要用get()方法传递参数"request"

    [html] view plaincopy
     
    1. public Object get(Object key)  
    2.   
    3. public Map getSession()  
    4.   
    5. public Map getApplication()  

    Action:

    [java] view plaincopy
     
    1. ActionContext context = ActionContext.getContext();  
    2. Map request=(Map)context.get("request");  
    3. Map session = context.getSession();  
    4. Map application = context.getApplication();  
    5.   
    6. request.put("greeting" "hello request");  
    7. session.put("user",user);  
    8.   
    9. Integer count = (Integer)application.get("counter");  
    10.   
    11.   
    12. application.put("counter",count);  


    JSP:

    [html] view plaincopy
     
    1. <h3>${sessionScope.user.username},${requestScope.greeting}。</h3><br />  
    2. ${applicationScope.counter}  


    利用请求对象来传递数据还有一种方式,可以直接利用ActionContext类的put()方法将数据保存到ActionContext中

    [java] view plaincopy
     
    1. Actioncontext.getContext().put("greeting","hello ");  

    然后在结果页面中,从请求对象中取出greeting属性

    [java] view plaincopy
     
    1. $(requestScope.greeting)或者<%=request.getAttribute("greeting")%>  

    ActionContext中保存的数据能够从请求对象中得到,其中的奥妙就在于Struct2中的org.apache.struts2.dispatcher.StrutsRequestWrapper类,这个类是HttpServletRequest的包装类,它重写了getAttribute()方法,在这个方法中,先请求对象中查找属性,如果没有,就从ActionContext中查找。这就是为什么ActionContext中保存的数据能够从请求对象中得到的原因。


    除了ActionContext来获取request,session和application对象这种方式外,Action类还可以实现某些特定接口,让Structs2在运行时向Action实例注入request,session,application对象。与之对应的三个接口和他们的方法:

    [java] view plaincopy
     
    1. org.apache.struts2.interceptor.RequestAware  
    2. org.apache.struts2.interceptor.SessionAware  
    3. org.apache.struts2.interceptor.ApplicationAware  
    [java] view plaincopy
     
    1. public class LoginAction implements Action,RequestAware,SessionAware,ApplicationAware{  
    2. private Map request;  
    3. private Map session;  
    4. private Map application;  
    5.   
    6. // ......  
    7.   
    8. @override  
    9. public void setRequest(Map request){  
    10. this.request=request;  
    11. }  
    12. @override  
    13. public void setSession(Map session){  
    14. this.session=session;  
    15. }  
    16. @override  
    17. public void setApplication(Map application){  
    18. this.application=application;  
    19. }  
    20.   
    21. }  

    与Servlet API耦合的访问方式

    要直接获取HttpServletRequest和ServletContext对象,可以使用org.apache.struts2.ServletActionContext类,在这个类中,定义了两个静态方法

    [java] view plaincopy
     
    1. public static HttpserlvetRequest getRequest()  
    2.   
    3. public static HttpservletResponse getResponse()  
    4.   
    5. pubolic static ServletContext getServletContext()  

    HttpSession对象可以通过HttpServletRequest对象来取到

    还可以调用ActionContext对象的get()方法,传递ServletActionContext.HTTP_REQUEST和ServletActionContext.SERVLET_CONTEXT键值来得到HttpServletRequest和ServletContext对象,如下:

    [java] view plaincopy
     
    1. ActionContext.getcontext().get(ServletActionContext.HTTP_REQUEST);  
    2. ActionContext.getcontext().get(ServletActionContext.HTTP_RESPONSE);  
    3. ActionContext.getcontext().get(ServletActionContext.SERVLET_CONTEXT);  
    [java] view plaincopy
     
    1. HttpServletResquest request  = ServletActionContext.getRequest();  
    2. HttpSession session = request.getSession();  
    3. ServletContext context = ServletActionContext.getServletContext();  
    4. //application中保存数据  
    5. Integer count = (Integer)context.getAttribute("counter");  

    除了利用ServletActionContext来获取HttpServletRequest对象和ServletContext对象这种方式外,Action类还可以实现ServletRequestAware和ServletContextAware接口,由Struts2向Action实例注入HttpServletRequest和ServletContext对象

    ServeletRequestAware接口和ServletContextAware接口不在同一个包,前者在org.apache.structs2.interceptor包中,后者在org.apache.struts2.util包中。

    [java] view plaincopy
     
      1. public class LoginAction implements Action,ServletRequestAware,ServletContextAware{  
      2. private HttpServletRequest request;  
      3. private ServletContext context;  
      4.   
      5. //......  
      6.   
      7. @override  
      8. public void setServletRequest(...){  
      9. //...  
      10. }  
      11.   
      12. @override  
      13. public void setServletContext(...){  
      14. //...  
      15. }  
      16. }  
  • 相关阅读:
    Redis实战(十)Redis常见问题及解决方案
    小团队构建大网站:中小研发团队架构实践
    Asp.net core 3.0
    图解TCP/IP
    TCP/IP协议
    Grid画边框
    WPF常用方法,事件驱动和控件遍历
    WPF中的画图
    WPF中的常用类汇总:
    WPF中的VisualTreeHelper
  • 原文地址:https://www.cnblogs.com/csy8fs/p/3939324.html
Copyright © 2011-2022 走看看