IT民工窝棚qbq.blog.chinaunix.net
qbq
全部博文(708)
国产(1)
欧美(1)
SEO(1)
CSS3(5)
TestNG(4)
HTML5(2)
iBatis(3)
URLRewrite(1)
WebService(1)
WebServer(12)
PHP(8)
OGNL(1)
AS2(2)
Multimedia(0)
Flex AS3(29)
面试(9)
Commet(1)
Ivy(2)
Bat(8)
Maven(18)
CSS(7)
Ext(9)
Spring问题集(4)
Word(1)
JFreeChart(2)
Groovy on Grails(14)
Python(1)
Portlet(3)
amCharts(4)
CSharp.NET(3)
Tools(1)
S2Dao(8)
HSQL(9)
taglib(28)
Source Safe(3)
JSTL(6)
EL(2)
Seasar-SAStruts(3)
Prototype(0)
JQuery(3)
DWR(7)
AJAX(14)
Guice(13)
Digit(2)
Notebook(4)
Log4J(8)
Servlet(2)
JSP(4)
Eclipse(12)
VB.NET(3)
DotNet(3)
JavaScript(63)
Thinking In Soft(10)
Framework(11)
English(0)
Struts2(14)
Struts(38)
Hibernate(10)
Spring(30)
HTML(14)
Web(5)
MYSQL(9)
SQLSERVER(1)
ORACLE(2)
SQL(3)
数据库(0)
DATABASE(0)
Windows(8)
JAVA(67)
Software(1)
Hardware(3)
OpenSource(2)
Microsoft(0)
Excel(4)
DIY(5)
Linux(4)
分类: Java
2008-05-21 08:51:31
在struts2中有两种方式可以得到这些对象1、非IoC方式要获得上述对象,关键Struts 2中com.opensymphony.xwork2.ActionContext类。我们可以通过它的静态方法getContext()获取当前Action的上下文对象。有了这个对象我们想获得其他几个对象就好办了 ActionContext ctx = ActionContext.getContext(); Map session = ctx.getSession(); 细心的朋友可以发现这里的session是个map对象在Struts2中底层的session都被封装成了Map类型我们可以直接操作这个map 进行对session的写入和读取操作而不用去直接操作HttpSession对象另外,org.apache.struts2.ServletActionContext作为辅助类(Helper Class),可以帮助您快捷地获得这几个对象。 HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); HttpSession session = request.getSession(); 如果你只是想访问session的属性(Attribute),你也可以通过ActionContext.getContext().getSession()获取或添加session范围(Scoped)的对象。2、IoC方式(这种方式在tapestry中也是这样实现的)要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。具体实现publicclass IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {private String message; private Map att; private HttpServletRequest request; private HttpServletResponse response; public String getMessage() {return message; }publicvoid setSession(Map att) {this.att = att; }publicvoid setServletRequest(HttpServletRequest request) {this.request = request; }publicvoid setServletResponse(HttpServletResponse response) {this.response = response; }@Overridepublic String execute() { att.put("msg", "Hello World from Session!"); HttpSession session = request.getSession(); StringBuffer sb =new StringBuffer("Message from request: "); sb.append(request.getParameter("msg")); sb.append("Response Buffer Size: "); sb.append(response.getBufferSize()); sb.append("Session ID: "); sb.append(session.getId()); message = sb.toString(); return SUCCESS; }}
上一篇:NBA球员的号码,绝对经典
下一篇:Struts2.0之Action详解(二)
登录 注册