[Struts2]訪問request,session和application對象(轉)

來源:互聯網
上載者:User

標籤:blog   http   java   使用   io   strong   ar   資料   art   

與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. }  

[Struts2]訪問request,session和application對象(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.