标签:des style blog http color io 使用 ar java
为避免与Servlet API耦合在一起,方便Action类做单元测试.Struts2对HttpServletRequest、HttpSession、ServletContext进行了封装,构造了三个Map对象来替代这三种对象。
注意,这三个对象与Servlet API中的三个对象是相同的。即,在Action中放入Session中信息,在JSP页面中是可以读出来的。
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
运行截图如下:
RequestAware接口:该接口中只有一个方法public void setRequest(Map<String,Object> request)SessionAware接口:该接口中只有一个方法public void setSession (Map<String,Object> session)ApplicationAware接口:该接口中只有一个方法public void setApplication (Map<String,Object> application)
${requestScope.req} <br/>${sessionScope.ses} <br/>${applicationScope.app}
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 setApplication(Map<String, Object> request) { this.request=request; } public void setSession(Map<String, Object> session) { this.session=session; } public void setRequest(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"; } }
9.Struts2在Action中获取request-session-application对象
标签:des style blog http color io 使用 ar java
原文地址:http://www.cnblogs.com/xingyunblog/p/4008379.html