1、XxxAware接口
ApplicationAware, RequestAware,SessionAware, ParameterAware.
struts2提供了这四个Aware接口用于Action类的实现,从而注入对应的application、request、session,parameter, 不过它们都是Map类型的。这和ActionContext一样是解耦的,即没有引入servlet相关的包,是在struts2内部的。
通过XxxAware接口的实现,可以方便的获取web资源。
2、测试代码
package com.test.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 TestAwareAction implements ApplicationAware, RequestAware,
SessionAware, ParameterAware {
public String execute() {
// 1、application application.put("applicationKey", "applicationValue");
System.out.println(application.get("date")); // 2、session session.put("sessionKey", "sessionValue"); // 3、request request.put("requestKey", "requestValue"); // 4、parameters
String[] vArr = (String[]) parameters.get("name");
System.out.println(vArr[0]); return "success";
} private Map<String, Object> application; @Override
public void setApplication(Map<String, Object> application) {
this.application = application;
} private Map<String, String[]> parameters; @Override
public void setParameters(Map<String, String[]> parameters) {
this.parameters = parameters;
} private Map<String, Object> session; @Override
public void setSession(Map<String, Object> session) {
this.session = session;
} private Map<String, Object> request; @Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
}