1. 修改Admin_add_success.jsp
<body>内容为:
Admin add success! <br>
<%=request.getAttribute("request")%><br>
<%=session.getAttribute("session")%><br>
<%=application.getAttribute("application")%><br>
<s:property value="#request.request"/><br>
<s:property value="#session.session"/><br>
<s:property value="#application.application"/><br>
2. 修改java类
package action;
import java.util.Map;
importorg.apache.struts2.interceptor.ApplicationAware;
importorg.apache.struts2.interceptor.RequestAware;
importorg.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class AdminAction extendsActionSupport implements RequestAware, SessionAware, ApplicationAware
{
private Map<String, Object> requestMap;
private Map<String, Object> requestActionMap;
private Map<String, Object> sessionMap;
private Map<String, Object> applicationMap;
public String add()
throws Exception
{
requestMap.put("request", "get request");
sessionMap.put("session", "get session");
applicationMap.put("application", "getapplication");
HttpServletRequestrequest = ServletActionContext.getRequest();
System.out.println("HttpServletRequest=" + request.getAttribute("request"));
requestActionMap = (Map<String, Object>)ActionContext.getContext().get("request");
System.out.println("ActionContext request=" + requestActionMap.get("request"));
return SUCCESS;
}
public void setApplication(Map map)
{
applicationMap = map;
}
public void setRequest(Map map)
{
requestMap = map;
}
public void setSession(Map map)
{
sessionMap = map;
}
}
3. 运行
1) 启动tomcat
访问: http://localhost:2000/Gao_struts2/struts/Admin_add.action
页面显示:
Admin add success!
get request
get session
get application
get request
get session
get application
控制台打印:
HttpServletRequest=get request
ActionContextrequest=get request
4. 总结
在action中操作request、session作用域有三种方式:
1:Action继承RequestAware(拦截器),实现setRequest方法获取Map<String, Object> requestMap
2:根据HttpServlet上下文:ServletActionContext.getRequest()获取HttpServletRequest
3:根据Action上下文:ActionContext.getContext().get("request")获取Map<String,Object> requestMap