1.编写Action类继承相应的接口:
package com.xuzhiwen.action; 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 AwareTest implements ApplicationAware,SessionAware,RequestAware,ParameterAware{ private Map<String,Object> applicationMap; private Map<String,Object> sessionMap; private Map<String,Object> requestMap; private Map<String,String[]> paramsMap; @Override public void setApplication(Map<String, Object> applicationMap) { this.applicationMap = applicationMap; } @Override public void setParameters(Map<String, String[]> paramsMap) { this.paramsMap = paramsMap; } @Override public void setRequest(Map<String, Object> requestMap) { this.requestMap = requestMap; } @Override public void setSession(Map<String, Object> sessionMap) { this.sessionMap = sessionMap; } }
四个接口的方法会被struts2框架自动调用。
这样就可以获取到四个对象了,并调用对象中相应的方法