码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2的Action中如何操作作用域对象

时间:2015-12-28 21:57:48      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

得到作用域对象有三种方法,这里用代码来解释:

 

技术分享
 1 package com.cy.action;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 
 7 import org.apache.struts2.interceptor.ServletRequestAware;
 8 import org.apache.struts2.interceptor.ServletResponseAware;
 9 import org.apache.struts2.util.ServletContextAware;
10 
11 import com.opensymphony.xwork2.ActionSupport;
12 
13 public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware {
14 
15     private static final long serialVersionUID = 1L;
16     private ServletContext servletContext;
17     private HttpServletRequest request;
18     private HttpServletResponse response;
19 
20     @Override
21     public void setServletContext(ServletContext arg0) {
22         // TODO Auto-generated method stub
23         this.servletContext=arg0;
24         
25     }
26 
27     @Override
28     public void setServletResponse(HttpServletResponse arg0) {
29         // TODO Auto-generated method stub
30         this.response=arg0;
31         
32     }
33 
34     @Override
35     public void setServletRequest(HttpServletRequest arg0) {
36         // TODO Auto-generated method stub
37         this.request=arg0;
38     }
39 
40     public HttpServletRequest getRequest() {
41         return request;
42     }
43 
44     public void setRequest(HttpServletRequest request) {
45         this.request = request;
46     }
47 
48     public HttpServletResponse getResponse() {
49         return response;
50     }
51 
52     public void setResponse(HttpServletResponse response) {
53         this.response = response;
54     }
55 
56     public ServletContext getServletContext() {
57         return servletContext;
58     }
59     
60     
61 
62 }
View Code

 

 

 1 //--------------Struts2 操作作用域的3种方式-------------------
 2 //        //采用实现接口的方式,来获取作用域(最常用)
 3 //        getRequest().setAttribute(arg0, arg1)
 4 //        getRequest().getSession().setAttribute(arg0, arg1)
 5 //        getServletContext().setAttribute(arg0, arg1)
 6 //        
 7         
 8 //        //采用ActionContext对象来对作用域进行操作
 9 //        ActionContext ac = ActionContext.getContext();
10 //        ac.put(key, value);//直接对request进行操作。
11 //        ac.getSession().put(key, value)//向Session中设值
12 //        ac.getApplication().put(key, value)//向ServletContext中设值
13         
14         
15 //        //采用ServletActionContext对象来对作用域进行操作
16 //        HttpServletRequest request = ServletActionContext.getRequest();
17 //        HttpServletResponse response = ServletActionContext.getResponse();
18 //        ServletContext sevletContext = ServletActionContext.getServletContext();
19 //    

随着技术不断的更新,这里的仅供参考!(Struts在公司里已经不再是主流了!)

Struts2的Action中如何操作作用域对象

标签:

原文地址:http://www.cnblogs.com/hellokitty1/p/5083716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!