struts2-与Servlet API解耦
与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 等.
a) 获取 HttpServletRequest 对应的 Map 对象
•public Object get(Object key): ActionContext 类中没有提供类似 getRequest() 这样的方法来获取 HttpServletRequest 对应的 Map 对象.
要得到 HttpServletRequest 对应的 Map 对象, 可以通过为 get() 方法传递 “request” 参数实现
b) 获取 HttpSession 对应的 Map 对象:
c) 获取 ServletContext 对应的 Map 对象:
1 import java.util.Map; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 7 import org.apache.struts2.ServletActionContext; 8 9 import com.opensymphony.xwork2.ActionSupport; 10 11 @SuppressWarnings("serial") 12 public class ContextAction extends ActionSupport { 13 14 public String test(){ 15 System.out.println("ContextAction*********test()"); 16 17 HttpServletRequest request = ServletActionContext.getRequest(); 18 request.setAttribute("username", "username_request"); 19 HttpServletResponse response = ServletActionContext.getResponse(); 20 21 Map sessionMap = ServletActionContext.getContext().getSession(); 22 sessionMap.put("username","username_session"); 23 24 ServletContext sc = ServletActionContext.getServletContext(); 25 sc.setAttribute("username","username_context"); 26 27 return "attr"; 28 } 29 }
如果你只是想访问session的属性(Attribute),你也可以通过ActionContext.getContext().getSession()获取
方法二:实现指定接口,由struts框架运行时注入:
Action 类通过可以实现某些特定的接口, 让Struts2 框架在运行时向 Action 实例注入parameters, request, session 和 application 对应的 Map 对象:
1 import java.util.Map; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 7 import org.apache.struts2.ServletActionContext; 8 import org.apache.struts2.interceptor.ServletRequestAware; 9 import org.apache.struts2.interceptor.ServletResponseAware; 10 import org.apache.struts2.interceptor.SessionAware; 11 import org.apache.struts2.util.ServletContextAware; 12 13 import com.opensymphony.xwork2.ActionSupport; 14 15 @SuppressWarnings("serial") 16 public class ContextAction2 extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware,SessionAware{ 17 18 HttpServletRequest request; 19 HttpServletResponse response; 20 Map sessionMap; 21 ServletContext sc; 22 23 public String test(){ 24 System.out.println("ContextAction*********test()"); 25 HttpServletRequest request = ServletActionContext.getRequest(); 26 request.setAttribute("username", "username_request"); 27 sessionMap.put("username","username_session"); 28 sc.setAttribute("username","username_context"); 29 return "attr"; 30 } 31 @Override 32 public void setSession(Map<String, Object> session) { 33 this.sessionMap =session; 34 } 35 @Override 36 public void setServletContext(ServletContext context) { 37 this.sc = context; 38 } 39 @Override 40 public void setServletResponse(HttpServletResponse response) { 41 this.response = response; 42 } 43 @Override 44 public void setServletRequest(HttpServletRequest request) { 45 46 } 47 }
struts_context.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <package name="context" namespace="/context" extends="struts-default"> 7 <action name="contextAction_test" class="cn.zengfansheng.struts.context.ContextAction" method="test"> 8 <result name="success"> 9 <param name="location">/context/success.jsp</param> 10 </result> 11 <result name="attr">/context/attr.jsp</result> 12 </action> 13 <action name="contextAction2_test" class="cn.zengfansheng.struts.context.ContextAction2" method="test"> 14 <result name="success"> 15 <param name="location">/context/success.jsp</param> 16 </result> 17 <result name="attr">/context/attr.jsp</result> 18 </action> 19 </package> 20 </struts>
attr.sjp
1 <%@ page language="java" pageEncoding="utf-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s"%> 3 <html> 4 <head> 5 <title>My JSP 'index.jsp' starting page</title> 6 <meta http-equiv="description" content="This is my page"> 7 </head> 8 <body> 9 ${requestScope.username}<br> 10 ${sessionScope.username}<br> 11 ${applicationScope.username}<br> 12 </body> 13 </html>