在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式。分别为servlet 不相关的 非IoC 取得Request等对象的方式servlet 不相关的 IoC 取得Request等对象的方式servlet 相关的 非IoC 取得Request等对象的方式servlet 相关的 IoC 取得Request等对象的方式以下分别叙述。首先请看struts.xml文件文件内容:
其中的LoginAction,Login2Action,Login3Action,Login4Action分别对应四种方式的Action。
文件名:result.jsp
文件内容:
文件名:getrequest.jsp
文件内容:
然后分别叙述4种方式,
1,servlet 不相关的 非IoC 取得Request等对象的方式
这种方法先取得ActionContext,然后获取Map类型的request等对象
Action文件:
2,servlet 不相关的 IoC 取得Request等对象的方式
这种方法,是实现特定的接口,由container来设定request等对象。请注意如下代码示例中红色文字的部分。
Action文件:
3,servlet 相关的 非IoC 取得Request等对象的方式
这种方法可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
取出方法,请参看代码中的蓝字部分。
Action文件:
4,servlet 相关的 IoC 取得Request等对象的方式
这种方法也可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
但是取出方法是通过接口进行实现,也就是由struts2的container来设定,请参看代码中的红字部分。
Action文件:
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"> |
05 |
06 | <struts> |
07 |
08 | <package
name="default"
extends="struts-default"> |
09 | <action
name="login"
class="struts2.login.LoginAction"> |
10 | <result
name="success"
>result.jsp</result> |
11 | </action> |
12 | |
13 | <action
name="login2"
class="struts2.login.Login2Action"> |
14 | <result
name="success"
>result.jsp</result> |
15 | </action> |
16 | |
17 | <action
name="login3"
class="struts2.login.Login3Action"> |
18 | <result
name="success"
>result.jsp</result> |
19 | </action> |
20 | |
21 | <action
name="login4"
class="struts2.login.Login4Action"> |
22 | <result
name="success"
>result.jsp</result> |
23 | </action> |
24 | </package> |
25 | |
26 | </struts> |
其中的LoginAction,Login2Action,Login3Action,Login4Action分别对应四种方式的Action。
文件名:result.jsp
文件内容:
1 | <%@ page contentType="text/html; charset=gb2312" %> |
2 | <%@ taglib uri="/struts-tags" prefix="s"%> |
3 |
4 | <html> |
5 | request 属性是 ${requestScope.attribute}<br> |
6 | session 属性是 ${sessionScope.attribute}<br> |
7 | application 属性是 ${applicationScope.attribute}<br> |
8 | </html> |
文件名:getrequest.jsp
文件内容:
1 | <%@ page contentType="text/html; charset=gb2312" %> |
2 | <%@ taglib uri="/struts-tags" prefix="s"%> |
3 | <html> |
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> |
8 | </form> |
9 | </html> |
然后分别叙述4种方式,
1,servlet 不相关的 非IoC 取得Request等对象的方式
这种方法先取得ActionContext,然后获取Map类型的request等对象
Action文件:
01 | package struts2.login; |
02 |
03 | import java.util.Map; |
04 |
05 | import com.opensymphony.xwork2.ActionContext; |
06 | import com.opensymphony.xwork2.ActionSupport; |
07 |
08 | public class
LoginAction extends
ActionSupport{ |
09 | private
ActionContext context; |
10 | private
Map request; |
11 | private
Map session; |
12 | private
Map application; |
13 | |
14 | @Override |
15 | public
String execute() throws
Exception { |
16 | // TODO Auto-generated method stub |
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 "); |
24 | return
SUCCESS; |
25 | } |
26 | } |
2,servlet 不相关的 IoC 取得Request等对象的方式
这种方法,是实现特定的接口,由container来设定request等对象。请注意如下代码示例中红色文字的部分。
Action文件:
01 | package struts2.login; |
02 |
03 | import java.util.Map; |
04 |
05 | import org.apache.struts2.interceptor.ApplicationAware; |
06 | import org.apache.struts2.interceptor.RequestAware; |
07 | import org.apache.struts2.interceptor.SessionAware; |
08 |
09 | import com.opensymphony.xwork2.ActionSupport; |
10 |
11 | public class
Login2Action extends
ActionSupport |
12 | implements
RequestAware , SessionAware, ApplicationAware{ |
13 | private
Map request; |
14 | private
Map session; |
15 | private
Map application; |
16 | |
17 | @Override |
18 | public
String execute() throws
Exception { |
19 | // TODO Auto-generated method stub |
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 "); |
23 | return
SUCCESS; |
24 | } |
25 |
26 | @Override |
27 | public
void setRequest(Map<String, Object> arg0) { |
28 | // TODO Auto-generated method stub |
29 | this.request = arg0; |
30 | } |
31 |
32 | @Override |
33 | public
void setSession(Map<String, Object> arg0) { |
34 | // TODO Auto-generated method stub |
35 | this.session = arg0; |
36 | } |
37 |
38 | @Override |
39 | public
void setApplication(Map<String, Object> arg0) { |
40 | // TODO Auto-generated method stub |
41 | this.application = arg0; |
42 | } |
43 | } |
3,servlet 相关的 非IoC 取得Request等对象的方式
这种方法可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
取出方法,请参看代码中的蓝字部分。
Action文件:
01 | package struts2.login; |
02 |
03 | import javax.servlet.ServletContext; |
04 | import javax.servlet.http.HttpServletRequest; |
05 | import javax.servlet.http.HttpSession; |
06 |
07 | import org.apache.struts2.ServletActionContext; |
08 |
09 | import com.opensymphony.xwork2.ActionSupport; |
10 |
11 | public class
Login3Action extends
ActionSupport{ |
12 | private
HttpServletRequest request; |
13 | private
HttpSession session; |
14 | private
ServletContext application; |
15 | |
16 | @Override |
17 | public
String execute() throws
Exception { |
18 | // TODO Auto-generated method stub |
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 "); |
25 | return
SUCCESS; |
26 | } |
27 | |
28 | } |
4,servlet 相关的 IoC 取得Request等对象的方式
这种方法也可以获取servlet相关的request等对象,也就是说这种方式取出的request对象不单单是设定属性的作用,而是可以获取http相关的信息。
但是取出方法是通过接口进行实现,也就是由struts2的container来设定,请参看代码中的红字部分。
Action文件:
01 | package struts2.login; |
02 |
03 | import javax.servlet.ServletContext; |
04 | import javax.servlet.http.HttpServletRequest; |
05 | import javax.servlet.http.HttpSession; |
06 |
07 | import org.apache.struts2.interceptor.ServletRequestAware; |
08 | import org.apache.struts2.util.ServletContextAware; |
09 |
10 | import com.opensymphony.xwork2.ActionSupport; |
11 |
12 | public class
Login4Action extends
ActionSupport |
13 | implements
ServletRequestAware , ServletContextAware{ |
14 | private
HttpServletRequest request; |
15 | private
HttpSession session; |
16 | private
ServletContext application; |
17 | |
18 | @Override |
19 | public
String execute() throws
Exception { |
20 | // TODO Auto-generated method stub |
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 "); |
25 | return
SUCCESS; |
26 | } |
27 |
28 | @Override |
29 | public
void setServletRequest(HttpServletRequest arg0) { |
30 | // TODO Auto-generated method stub |
31 | this.request = arg0; |
32 | } |
33 |
34 | @Override |
35 | public
void setServletContext(ServletContext arg0) { |
36 | // TODO Auto-generated method stub |
37 | this.application = arg0; |
38 | } |
39 | } |
本文介绍Struts2框架中从Action获取request、session等对象的四种方法,包括servlet不相关的非IoC/IoC方式及servlet相关的非IoC/IoC方式,并提供具体代码示例。
2070

被折叠的 条评论
为什么被折叠?



