觉得清晰易了,就转来了,转载地址:http://www.open-open.com/lib/view/open1326954429562.html
在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式。分别为servlet 不相关的 非IoC 取得Request等对象的方式servlet 不相关的 IoC 取得Request等对象的方式servlet 相关的 非IoC 取得Request等对象的方式servlet 相关的 IoC 取得Request等对象的方式以下分别叙述。首先请看struts.xml文件文件内容:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
04 | "http://struts.apache.org/dtds/struts-2.0.dtd"> |
08 | < package name = "default" extends = "struts-default" > |
09 | < action name = "login" class = "struts2.login.LoginAction" > |
10 | < result name = "success" >result.jsp</ result > |
13 | < action name = "login2" class = "struts2.login.Login2Action" > |
14 | < result name = "success" >result.jsp</ result > |
17 | < action name = "login3" class = "struts2.login.Login3Action" > |
18 | < result name = "success" >result.jsp</ result > |
21 | < action name = "login4" class = "struts2.login.Login4Action" > |
22 | < result name = "success" >result.jsp</ result > |
其中的LoginAction,Login2Action,Login3Action,Login4Action分别对应四种方式的Action。
文件名:result.jsp
文件内容:
1 | <%@ page contentType="text/html; charset=gb2312" %> |
2 | <%@ taglib uri="/struts-tags" prefix="s"%> |
5 | request 属性是 ${requestScope.attribute}< br > |
6 | session 属性是 ${sessionScope.attribute}< br > |
7 | application 属性是 ${applicationScope.attribute}< br > |
文件名:getrequest.jsp
文件内容:
1 | <%@ page contentType="text/html; charset=gb2312" %> |
2 | <%@ taglib uri="/struts-tags" prefix="s"%> |
4 | < a href = "login.action" >servlet 不相关的 非IoC 取得Request等对象的方式</ a >< br > |
5 | < a href = "login2.action" >servlet 不相关的 IoC 取得Request等对象的方式</ a >< br > |
6 | < a href = "login3.action" >servlet 相关的 非IoC 取得Request等对象的方式</ a >< br > |
7 | < a href = "login4.action" >servlet 相关的 IoC 取得Request等对象的方式</ a >< br > |
然后分别叙述4种方式,
1,servlet 不相关的 非IoC 取得Request等对象的方式
这种方法先取得ActionContext,然后获取Map类型的request等对象
Action文件:
05 | import com.opensymphony.xwork2.ActionContext; |
06 | import com.opensymphony.xwork2.ActionSupport; |
08 | public class LoginAction extends ActionSupport{ |
09 | private ActionContext context; |
12 | private Map application; |
15 | public String execute() throws Exception { |
17 | this .context = ActionContext.getContext(); |
18 | this .request = (Map) this .context.get( "request" ); |
19 | this .session = this .context.getSession(); |
20 | this .application = this .context.getApplication(); |
21 | this .request.put( "attribute" , "request value servlet 不相关的 非IoC " ); |
22 | this .session.put( "attribute" , "session value servlet 不相关的 非IoC " ); |
23 | this .application.put( "attribute" , "application value servlet 不相关的非IoC " ); |
2,servlet 不相关的 IoC 取得Request等对象的方式
这种方法,是实现特定的接口,由container来设定request等对象。请注意如下代码示例中红色文字的部分。
Action文件:
05 | import org.apache.struts2.interceptor.ApplicationAware; |
06 | import org.apache.struts2.interceptor.RequestAware; |
07 | import org.apache.struts2.interceptor.SessionAware; |
09 | import com.opensymphony.xwork2.ActionSupport; |
11 | public class Login2Action extends ActionSupport |
12 | implements RequestAware , SessionAware, ApplicationAware{ |
15 | private Map application; |
18 | public String execute() throws Exception { |
20 | this .request.put( "attribute" , "request value servlet 不相关的 IoC " ); |
21 | this .session.put( "attribute" , "session value servlet 不相关的 IoC " ); |
22 | this .application.put( "attribute" , "application value servlet 不相关的 IoC " ); |
27 | public void setRequest(Map<String, Object> arg0) { |
33 | public void setSession(Map<String, Object> arg0) { |
39 | public void setApplication(Map<String, Object> arg0) { |
41 | this .application = arg0; |
3,servlet 相关的 非IoC 取得Request等对象的方式
这种方法可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
取出方法,请参看代码中的蓝字部分。
Action文件:
03 | import javax.servlet.ServletContext; |
04 | import javax.servlet.http.HttpServletRequest; |
05 | import javax.servlet.http.HttpSession; |
07 | import org.apache.struts2.ServletActionContext; |
09 | import com.opensymphony.xwork2.ActionSupport; |
11 | public class Login3Action extends ActionSupport{ |
12 | private HttpServletRequest request; |
13 | private HttpSession session; |
14 | private ServletContext application; |
17 | public String execute() throws Exception { |
19 | this .request = ServletActionContext.getRequest(); |
20 | this .session = this .request.getSession(); |
21 | this .application = ServletActionContext.getServletContext(); |
22 | this .request.setAttribute( "attribute" , "request value servlet 相关的非IoC " ); |
23 | this .session.setAttribute( "attribute" , "session value servlet 相关的非IoC " ); |
24 | this .application.setAttribute( "attribute" , "application value servlet 相关的 非IoC " ); |
4,servlet 相关的 IoC 取得Request等对象的方式
这种方法也可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
但是取出方法是通过接口进行实现,也就是由struts2的container来设定,请参看代码中的红字部分。
Action文件:
03 | import javax.servlet.ServletContext; |
04 | import javax.servlet.http.HttpServletRequest; |
05 | import javax.servlet.http.HttpSession; |
07 | import org.apache.struts2.interceptor.ServletRequestAware; |
08 | import org.apache.struts2.util.ServletContextAware; |
10 | import com.opensymphony.xwork2.ActionSupport; |
12 | public class Login4Action extends ActionSupport |
13 | implements ServletRequestAware , ServletContextAware{ |
14 | private HttpServletRequest request; |
15 | private HttpSession session; |
16 | private ServletContext application; |
19 | public String execute() throws Exception { |
21 | this .request.setAttribute( "attribute" , "request value servlet 相关的 IoC " ); |
22 | this .session = this .request.getSession(); |
23 | this .session.setAttribute( "attribute" , "session value servlet 相关的 IoC " ); |
24 | this .application.setAttribute( "attribute" , "application value servlet 相关的 IoC " ); |
29 | public void setServletRequest(HttpServletRequest arg0) { |
35 | public void setServletContext(ServletContext arg0) { |
37 | this .application = arg0; |