为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSession 和 ServletContext 进行了封装, 构造了 3 个 Map 对象来替代这 3 个对象, 在 Action 中可以直接使用 HttpServletRequest, HttpSession, ServletContext 对应的 Map 对象来保存和读取数据。这里大家注意,struts1是没有提供与Servlet的API解耦的。

1.Struts2如何获取request、response、session、ServletContext

  在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应(HttpServletResponse)操作.Struts2提供了三种用于获取这些对象的方法,下面来一一介绍一下;

非IOC方法:通过ActionContext,ServletActionContext类直接获取,实现与Servlet解耦

  • ActionContext是 Action 执行的上下文对象, 在 ActionContext 中保存了 Action 执行所需要的所有对象, 包括 parameters, request, session, application 等.
static ThreadLocal actionContext = new ActionContextThreadLocal()
(ActionContext) actionContext.get();来获取
  • l 获取 HttpServletRequest 对应的 Map 对象:public Object get(Object key): ActionContext 类中没有提供类似 getRequest() 这样的方法来获取 HttpServletRequest 对应的 Map 对象. 要得到 HttpServletRequest 对应的 Map 对象, 可以通过为 get() 方法传递 “request” 参数实现
ActionContext.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
  • 获取HTTPServletResponse,该获取同request相似;
ActionContext.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
  • l 获取 HttpSession 对应的 Map 对象:
public Map getSession()
  • l 获取 ServletContext 对应的 Map 对象:
public Map getApplication()

测试代码:获取request、response、session、application对象

@SuppressWarnings("serial")
public class ContextAction extends ActionSupport{ @Override
public String execute() throws Exception {
System.out.println("欢迎访问ContextAction中的execute方法!");
/**request对象(与servletAPI解耦的方式)*//*
ActionContext.getContext().put("username", "request_username");
*//**session对象(与servletAPI解耦的方式)*//*
ActionContext.getContext().getSession().put("username", "session_username");
*//**application对象(ServletContext对象)(与servletAPI解耦的方式)*//*
ActionContext.getContext().getApplication().put("username", "application_username");
*/
//使用与ServletActionContext的方式操作上述
        ServletActionContext.getRequest().setAttribute("username", "request_username");
        ServletActionContext.getServletContext().setAttribute("username", "application_username");
        ServletActionContext.getRequest().getSession().setAttribute("username", "session_username");
System.out.println("request:"+ServletActionContext.getRequest());
System.out.println("response:"+ServletActionContext.getResponse());
System.out.println("session:"+ServletActionContext.getRequest().getSession());
System.out.println("servletContext:"+ServletActionContext.getServletContext());
//Action接口中常量 SUCCESS="success"
return SUCCESS;
}
}

IOC方法:实现指定接口,由struts框架运行时注入,即使用Struts2 Aware拦截器

Action 类通过可以实现某些特定的接口, 让Struts2 框架在运行时向 Action 实例注入 parameters, request, session 和 application 对应的 Map 对象:

测试代码::获取request、response、session、application对象

@SuppressWarnings("serial")
public class ContextActionTwo extends ActionSupport implements ServletRequestAware,
ServletContextAware,ServletResponseAware,SessionAware{
private HttpServletRequest request;
private HttpServletResponse response;
private Map<String, Object> session;
private ServletContext application;//注意可以实现接口ApplicationAware同样的功能
//使用第二种方式
@Override
public String execute() throws Exception {
System.out.println("欢迎访问ContextActionTwo中的execute()!");
//向作用域中存值
request.setAttribute("username", "request_username2");
session.put("username", "session_username2");
application.setAttribute("username", "application_username2");
return SUCCESS;
} @Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
} @Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response = response;
} @Override
public void setServletContext(ServletContext context) {
// TODO Auto-generated method stub
this.application = context;
} @Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
}
attr.jsp  

<body>123
${requestScope.username}<br>
${sessionScope.username}<br>
${applicationScope.username}<br>
</body>

struts-context.xml文件的配置:

<struts>
<package name="context" namespace="/context" extends="struts-default">
<default-action-ref name="contextAction_test"></default-action-ref> <action name="contextAction_test" class="cn.youric.you.two_context.ContextAction">
<result name="success">/context/success.jsp</result>
</action>
<action name="contextAction02_test" class="cn.youric.you.two_context.ContextActionTwo">
<result name="success">/context/attr.jsp</result>
</action>
</package>
</struts>

别忘了include;

2.聊聊ActionContext

  ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象. 一般情况, 我们的ActionContext都是通过: ActionContext context = (ActionContext) actionContext.get();来获取的.我们再来看看这里的actionContext对象的创建:

static ThreadLocal<ActionContext> actionContext = new ThreadLocal<>();

  ThreadLocal可以命名为"线程局部变量",它为每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.这样,我们ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的.

3. ServletActionContext和ActionContext联系

  ServletActionContext和ActionContext有着一些重复的功能,在我们的Action中,该如何去抉择呢?我们遵循的原则是:如果ActionContext能够实现我们的功能,那最好就不要使用ServletActionContext,让我们的Action尽量不要直接去访问Servlet的相关对象.

  注意:在使用ActionContext时有一点要注意: 不要在Action的构造函数里使用ActionContext.getContext(),因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null;同样,HttpServletRequest req = ServletActionContext.getRequest()也不要放在构造函数中,也不要直接将req作为类变量给其赋值。至于原因,我想是因为前面讲到的static ThreadLocal actionContext = new ActionContextThreadLocal(),从这里我们可以看出ActionContext是线程安全的,而ServletActionContext继承自ActionContext,所以ServletActionContext也线程安全,线程安全要求每个线程都独立进行,所以req的创建也要求独立进行,所以ServletActionContext.getRequest()这句话不要放在构造函数中,也不要直接放在类中,而应该放在每个具体的方法体中(eg:login()、queryAll()、insert()等),这样才能保证每次产生对象时独立的建立了一个req。

最新文章

  1. MVC项目内无法添加System.Web.Optimization
  2. 浅入ARP
  3. 一个简单的TestNG例子
  4. qppium 原理解析
  5. SDOI2017 BZOJ 4820 硬币游戏 解题报告
  6. [物理学与PDEs]第1章第9节 Darwin 模型 9.1 拟静电模型及其修正形式
  7. nginx php程序 文件上传大小限制
  8. DIV CSS float浮动
  9. 【转】MySQL— 基础
  10. linux之sed的使用
  11. S5PV210 时钟体系分析
  12. Android之官方导航栏ActionBar
  13. Mac下启动AndroidStudio失败
  14. PyQt5系列教程(九)QInputDialog的使用
  15. node服务端搭建学习笔记
  16. 程序移植到AUTOCAD2013笔记
  17. G - Ice_cream&#39;s world I (并查集)
  18. BZOJ054_移动玩具_KEY
  19. UVa 1329 - Corporative Network Union Find题解
  20. Java中equals()、equalsIgnoreCase()和==的区别

热门文章

  1. FontAwesome 图标 class=&quot;fa fa-home&quot;
  2. SQL 脚本整理 笔记
  3. Wpf ListView展示风格
  4. Node 的fs模块
  5. 001Angular2环境准备
  6. Android layout 布局 属性详解
  7. IOS APP 瘦身
  8. network embedding 需读论文
  9. 多线程(三)~多线程中数据的可见性-volatile关键字
  10. spring----spring中的注解@service等的作用