为避免与Servlet API耦合在一起,方便Action类做单元测试.Struts2对HttpServletRequest、HttpSession、ServletContext进行了封装,构造了三个Map对象来替代这三种对象。
注意,这三个对象与Servlet API中的三个对象是相同的。即,在Action中放入Session中信息,在JSP页面中是可以读出来的。
方式一:通过使用ActionContext类获取
在Struts2框架中,通过Action的执行上下文类ActionContext,可以获取request/session/application对象。
在Struts2框架中,通过Action的执行上下文类ActionContext,可以获取request/session/application对象。
ActionContext对象本身就是request范围的存储空间。
所以,对于向request范围中添加属性,直接向ActionContext对象中添加即可。
ActionContext ctx = ActionContext.getContext();
Map<String,Object> application = ctx.getApplication();
Map<String,Object> session = ctx.getSession();application.put(“app”, “应用范围”);
//往ServletContext里放入app session.put(“ses”, “session范围”);
//往session里放入ses ctx.put(“req”, “request范围”);
//往request里放入req
代码文档目录如下:
index.jsp源码如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
This is my JSP page. <br>
</body>
</html>
show.jsp源码如下:
<%@ page pageEncoding="utf-8" isELIgnored="false"%> <html>
<head>
<title>show page</title>
</head> <body> application中的app=${applicationScope.app}<br/>
session中的ses=${sessionScope.ses}<br/>
request中的req=${requestScope.req} </body>
</html>
ScopeTestAction.java源码如下:
package actions; import java.util.Map; import com.opensymphony.xwork2.ActionContext; public class ScopeTestAction {
public String execute(){
ActionContext ctx=ActionContext.getContext();
Map<String,Object> application=ctx.getApplication();
Map<String,Object> session=ctx.getSession(); application.put("app","app scope");
session.put("ses","ses scope"); //request中放入属性req
ctx.put("req", "req scope"); return "success";
} }
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="one" extends="struts-default"> <action name="scope" class="actions.ScopeTestAction">
<result>/show.jsp</result>
</action> </package> </struts>
部署发布,启动tomcat,输入地址:
http://127.0.0.1:8080/request_session_application/scope.action
运行截图如下:
方式二:通过实现特定接口来获取
通过让Action实现特定接口,也可获取request/session/application对象。
RequestAware接口:该接口中只有一个方法public void setRequest(Map<String,Object> request)SessionAware接口:该接口中只有一个方法public void setSession (Map<String,Object> session)ApplicationAware接口:该接口中只有一个方法public void setApplication (Map<String,Object> application)
只要在Action中以如下的形式定义好相应的对象:
Map<String,Object> request;
并以如下的方式实现特定接口中的方法:
this.request = request;
那么,在Action方法中(如execute方法)就可使用相应的对象了。
对这些对象的访问:
在各范围中放入属性后,在页面中即可通过EL表达式将其值读出来了。
${requestScope.req} <br/>${sessionScope.ses} <br/>${applicationScope.app}
实例:在Action中获取request/session/application—scopetest
Step1:编写ScopeTestAction与ScopeTestAction2类,来实现使用两种方式的添加相应习属性的功能
Step2:编写web.xml与struts.xml
Step3:编写show.jsp
在上面的基础上新建ScopeTestAction2.java
完成源码如下:
package actions; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware; public class ScopeTestAction2 implements RequestAware, SessionAware,ApplicationAware {
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application; public void setRequest(Map<String, Object> request) {
this.request = request;
} public void setSession(Map<String, Object> session) {
this.session = session;
} public void setApplication(Map<String, Object> application) {
this.application = application;
} public String execute(){ application.put("app","app scope");
session.put("ses","ses scope"); //request中放入属性req
request.put("req", "req scope");
return "success";
} }
修改后struts.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="one" extends="struts-default"> <action name="scope" class="actions.ScopeTestAction">
<result>/show.jsp</result>
</action>
<action name="scope2" class="actions.ScopeTestAction2">
<result>/show.jsp</result>
</action> </package> </struts>
重新部署发布,启动tomcat,输入地址:
http://127.0.0.1:8080/request_session_application/scope2.action