通过ActionContext访问
public class TestActionContextAction {
public String execute(){
//获取 ActionContext 对象
ActionContext actionContext = ActionContext.getContext();
//application
Map<String, Object> applicationMap = actionContext.getApplication();
applicationMap.put("applicationKey", "applicationValue");
//获取属性
Object date = applicationMap.get("date");
System.out.println("date: " + date);
//2. session
Map<String, Object> sessionMap = actionContext.getSession();
sessionMap.put("sessionKey", "sessionValue");
//清空session
/*if(sessionMap instanceof SessionMap){
SessionMap sm = (SessionMap) sessionMap;
sm.invalidate();
System.out.println("session 失效了. ");
}*/
//request
Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request");
requestMap.put("requestKey", "requestValue");
//parameter
Map<String, Parameter> parameters = actionContext.getParameters();
System.out.println(parameters.get("name"));
return "success";
}
}
Aware方式
public class TestAwareAction implements ApplicationAware, SessionAware, RequestAware
,ParameterAware{
public String execute(){
//1. 向 application 中加入一个属性: applicationKey2 - applicationValue2
application.put("applicationKey", "applicationValue");
//2. 从 application 中读取一个属性 date, 并打印.
System.out.println(application.get("date"));
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
return "success";
}
public String save(){
return null;
}
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> request;
@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = request;
}
private Map<String, Object> session;
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
}
public class TestServletAwareAction implements ServletRequestAware,
ServletResponseAware,ServletContextAware{
public String execute() {
request.setAttribute("requestKey", "requestValue");
request.getSession().setAttribute("sessionKey", "sessionValue");
System.out.println(request.getParameter("name"));
return "success";
}
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
// TODO Auto-generated method stub
this.servletContext = servletContext;
}
private HttpServletResponse response;
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response = response;
}
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
}
使用ServletActionContext
public class TestServletActionContext {
public String execute() {
ServletRequest request = ServletActionContext.getRequest();
ServletResponse response = ServletActionContext.getResponse();
ServletContext con = ServletActionContext.getServletContext();
String name = request.getParameter("name");
return "success";
}
}
jsp页面
application : ${applicationScope.applicationKey }
<br><br>
session: ${sessionScope.sessionKey }
<br><br>
request: ${requestScope.requestKey }
<br><br>
parameters ${parameters.name }
struts.xml
<action name="TestActionContext" class="com.TestActionContextAction">
<result name="success">/actionContext.jsp</result>
</action>
<action name="TestAware" class="com.TestAwareAction">
<result>/aware.jsp</result>
</action>
<action name="TestServletAwareAction"
class="com.TestServletAwareAction">
<result name="success">/aware.jsp</result>
</action>