使用XxxAware接口
看代码:
package logan.struts2.study; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; public class TestAwareAction implements ApplicationAware{ public String execute(){ //1.向application中加入一个属性:applicationKey2 - applicationValue2 application.put("applicationKey2", "applicationValue2"); //2.从application中读取一个属性,date,并打印。 System.out.println(application.get("date")); return "success"; } private Map<String,Object> application; @Override public void setApplication(Map<String, Object> application) { // TODO Auto-generated method stub this.application = application; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- action VS Action类 action:代表一个Struts2的一个请求 Action类:能够处理Struts2请求的类 --> <package name="default" namespace="/" extends="struts-default"> <action name="TestActionContext" class="logan.struts2.study.TestActionContext"> <result>/test-actionContext.jsp</result> </action> <action name="TestAware" class="logan.struts2.study.TestAwareAction"> <result>/test-aware.jsp</result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Struts2-2</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
index.jsp
<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <a href="TestActionContext.action?name=logan&name=logan2">Test ActionContext</a> <br><br> <a href="TestAware.action?name=logan">Test Aware</a> <% if(application.getAttribute("date") == null){ application.setAttribute("date", new Date()); } %> </body> </html>
test-aware.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h4>Test Aware Page</h4> application:${applicationScope.applicationKey2 } </body> </html>
访问地址:
http://localhost:8080/Struts2-3/index.jsp
还可以这样写:
package logan.struts2.study; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.ParameterAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; public class TestAwareAction implements ApplicationAware,SessionAware,RequestAware{ public String execute(){ //1.向application中加入一个属性:applicationKey2 - applicationValue2 application.put("applicationKey2", "applicationValue2"); //2.从application中读取一个属性,date,并打印。 System.out.println(application.get("date")); return "success"; } private Map<String,Object> application; @Override public void setApplication(Map<String, Object> application) { // TODO Auto-generated method stub this.application = application; } @Override public void setRequest(Map<String, Object> arg0) { // TODO Auto-generated method stub } @Override public void setSession(Map<String, Object> arg0) { // TODO Auto-generated method stub } }
选用建议:若一个Action类中有多个action方法,且多个方法都需要使用域对象的Map或parameters,则建议使用接口的方式。
如下修改代码:
package logan.struts2.study; import java.util.Map; import org.apache.struts2.dispatcher.Parameter; import org.apache.struts2.dispatcher.SessionMap; import com.opensymphony.xwork2.ActionContext; public class TestActionContext { public String execute(){ //0.获取ActionContext对象 //ActionContext是Action的上下文对象,可以从中获取到当前Action需要的一切信息 ActionContext actionContext = ActionContext.getContext(); //1.获取application对应的Map,并想其中添加一个属性 //通过调用ActionContext 对象的getApplication()方法来获取application对象的Map对象 Map<String, Object> applicationMap = actionContext.getApplication(); //设置属性 applicationMap.put("applicationKey", "applicationValue"); //获取属性 Object date = applicationMap.get("date"); System.out.println(date); //2.session Map<String,Object> sessionMap = actionContext.getSession(); sessionMap.put("sessionKey", "sessionValue"); if(sessionMap instanceof SessionMap){ SessionMap sm = (SessionMap) sessionMap; sm.invalidate(); System.out.println("session 失效了"); } //3.request //ActionContext中并没有提供getRequest方法来获取Request对应的Map //需要手工调用get()方法,传入request字符串来获取。 Map<String,Object> requestMap = (Map<String, Object>) actionContext.get("request"); requestMap.put("requestKey", "requestValue"); //4.获取请求参数对应的Map,并获取指定的参数值 //parameters这个Map只能读,不能写。如果写入,不会报错,但是也不起作用。 Map<String,Parameter> parameters = actionContext.getParameters(); System.out.println(parameters.get("name")); return "success"; } }
这样就获取不到session
sessionMap对应的Map实际上是SessionMap类型的,强转后若调用其invalidate()方法,可以使其session失效。