与Servlet API解耦的访问方式
为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSession 和 ServletContext 进行了封装, 构造了 3 个 Map 对象来替代这 3 个对象, 在 Action 中可以直接使用 HttpServletRequest, HttpSession, ServletContext 对应的 Map 对象来保存和读取数据.
访问request/session/application对象
方法一:通过ServletActionContext类直接获取
- ActionContext 是 Action 执行的上下文对象, 在 ActionContext 中保存了 Action 执行所需要的所有对象, 包括 parameters, request, session, application 等.
- 获取 HttpServletRequest 对应的 Map 对象:
- public Object get(Object key): ActionContext 类中没有提供类似 getRequest() 这样的方法来获取 HttpServletRequest 对应的 Map 对象. 要得到 HttpServletRequest 对应的 Map 对象, 可以通过为 get() 方法传递 “request” 参数实现
- 获取 HttpSession 对应的 Map 对象:
- public Map getSession()
- 获取 ServletContext 对应的 Map 对象:
- public Map getApplication()
ContextAction文件:
package cn.itcast.context;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ContextAction extends ActionSupport {
public String test(){
System.out.println("ContextAction *********** test()");
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("username", "username_request");
HttpServletResponse response = ServletActionContext.getResponse();
Map sessionMap = ServletActionContext.getContext().getSession();
sessionMap.put("username", "username_session");
ServletContext sc = ServletActionContext.getServletContext();
sc.setAttribute("username", "username_application");
return "attr";
}
}
struts_context.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>
<package name="context" namespace="/context" extends="struts-default">
<action name="contextAction_test" class="cn.itcast.context.ContextAction" method="test">
<result name="success">/context/success.jsp</result>
<result name="attr">/context/attr.jsp</result>
</action>
</package>
</struts>
struts.xml配置
<include file="cn/itcast/context/struts_context.xml"></include>
jsp页面:
test.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/context/contextAction_test.action">testContext</a><br>
<a href="${pageContext.request.contextPath}/context/contextAction02_test.action">testContext</a><br>
</body>
</html>
attr.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="description" content="This is my page">
</head>
<body>
${requestScope.username}<br>
${sessionScope.username}<br>
${applicationScope.username}<br>
</body>
</html>
测试:
方法二:实现指定接口,由struts框架运行时注入
Action 类通过可以实现某些特定的接口, 让Struts2 框架在运行时向 Action 实例注入 parameters, request, session 和 application 对应的 Map 对象:
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
ContextAction02 文件
package cn.itcast.context;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ContextAction02 extends ActionSupport
implements ServletRequestAware, ServletResponseAware, ServletContextAware, SessionAware{
HttpServletRequest request;
HttpServletResponse response;
Map sessionMap;
ServletContext sc;
public String test(){
System.out.println("ContextAction *********** test()");
request.setAttribute("username", "username_request");
sessionMap.put("username", "username_session");
sc.setAttribute("username", "username_application");
return "attr";
}
public void setSession(Map<String, Object> session) {
this.sessionMap = session;
}
public void setServletContext(ServletContext context) {
this.sc = context;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
struts_context.xml配置:
<action name="contextAction02_test" class="cn.itcast.context.ContextAction02" method="test">
<result name="attr">/context/attr.jsp</result>
</action>
测试: