struts2中 ServletActionContext与ActionContext区别
1. ActionContext
在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应(HttpServletResponse)操作. 我们需要在Action中取得request请求参数"username"的值:
- ActionContext context = ActionContext.getContext();
- Map params = context.getParameters();
- String username = (String) params.get("username");
ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象. 一般情况, 我们的ActionContext都是通过: ActionContext context = (ActionContext) actionContext.get();来获取的.我们再来看看这里的actionContext对象的创建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是实现ThreadLocal的一个内部类.ThreadLocal可以命名为"线程局部变量",它为每一个使用该变量的线程都提供一个变量值的副本,使每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.这样,我们ActionContext里的属性只会在对应的当前请求线程中可见,从而保证它是线程安全的.
通过ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();
2. ServletActionContext
ServletActionContext(com.opensymphony.webwork. ServletActionContext),这个类直接继承了我们上面介绍的ActionContext,它提供了直接与Servlet相关对象访问的功能,它可以取得的对象有:
(1)javax.servlet.http.HttpServletRequest : HTTPservlet请求对象
(2)javax.servlet.http.HttpServletResponse : HTTPservlet相应对象
(3)javax.servlet.ServletContext : Servlet上下文信息
(4)javax.servlet.ServletConfig : Servlet配置对象
(5)javax.servlet.jsp.PageContext : Http页面上下文
如何从ServletActionContext里取得Servlet的相关对象:
<1>取得HttpServletRequest对象: HttpServletRequest request = ServletActionContext. getRequest();
<2>取得HttpSession对象: HttpSession session = ServletActionContext. getRequest().getSession();
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。
4. struts2中获得request、response和session
(1)非IoC方式
方法一:使用org.apache.struts2.ActionContext类,通过它的静态方法getContext()获取当前Action的上下文对
<pre name="code" class="java">ActionContext ctx = ActionContext.getContext();
ctx.put("liuwei", "andy"); //request.setAttribute("liuwei", "andy");
Map session = ctx.getSession(); //session
HttpServletRequest request = ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
HttpServletResponse response = ctx.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);</pre><br>
<p></p>
<pre></pre>
<br>
<br>
<p></p>
<p><span style="font-size:16px">细心的朋友可以发现这里的session是个Map对象, 在Struts2中底层的session都被封装成了Map类型.
<br>
</span></p>
<p><span style="font-size:16px">我们可以直接操作这个Map对象进行对session的写入和读取操作, 而不用去直接操作HttpSession对象.</span></p>
<p><span style="font-size:16px">方法二:使用org.apache.struts2.ServletActionContext类</span></p>
<p><strong></strong></p><pre name="code" class="java"> public class UserAction extends ActionSupport {
//其他代码片段
private HttpServletRequest req;
// private HttpServletRequest req = ServletActionContext.getRequest(); 这条语句放在这个位置是错误的,同样把这条语句放在构造方法中也是错误的。
public String login() {
req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现
user = new User();
user.setUid(uid);
user.setPassword(password);
if (userDAO.isLogin(user)) {
req.getSession().setAttribute("user", user);
return SUCCESS;
}
return LOGIN;
}
public String queryAll() {
req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现
uList = userDAO.queryAll();
req.getSession().setAttribute("uList", uList);
return SUCCESS;
}
//其他代码片段
}</pre><br>
<br>
<p></p>
<p><span style="font-size:16px"><strong>(2)IoC方式(即使用Struts2 Aware拦截器)</strong></span></p>
<p>要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。</p>
<pre name="code" class="java"> public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String execute() {
HttpSession session = request.getSession();
return SUCCESS;
}
}</pre><br>
<br>
struts2中 ServletActionContext与ActionContext区别的更多相关文章
- struts2 的 ServletActionContext 和 actionContext,服务器代码测试, redirect 、dispatcher、chain、redirectAction
一.ServletActionContext 和 actionContext HttpServletRequest request=ServletActionContext.getRequest() ...
- Struts2中 ValueStack、ActionContext、ServletContext、request、session等 表格解析
变量 从ActionContext中获得 生命周期 用Ongl来读取值 使用ServletConfigInterceptor来注入 ActionContext类 静态方法ActionContext. ...
- Struts2中ActionContext和ServletActionContext
转自:http://blog.sina.com.cn/s/blog_6c9bac050100y9iw.html 在Web应用程序开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在A ...
- Struts2中ActionContext及ServletActionContext介绍(转载)
1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...
- 简单理解Struts2中拦截器与过滤器的区别及执行顺序
简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...
- 在Struts2中使用ValueStack、ActionContext、ServletContext、request、session等 .
笔者不知道该用哪个词来形容ValueStack.ActionContext等可以在Struts2中用来存放数据的类.这些类使用的范围不同,得到的方法也不同,下面就来一一介绍. 声明:本文参考Strut ...
- ActionContext介绍(在Struts2中)
一种属性的有序序列,它们为驻留在环境内的对象定义环境.在对象的激活过程中创建上下文,对象被配置为要求某些自动服务,如同步.事务.实时激活.安全性等等.多个对象可以存留在一个上下文内.也有根据上下文理解 ...
- struts2中拦截器与过滤器之间的区别
首先是一张经典的struts2原理图 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标准的过滤器链 c) ...
- Struts2中的ActionContext、OGNL及EL的使用
文章分类:Java编程 本文基于struts2.1.8.1,xwork2.1.6 1.EL EL(Expression Language)源于jsp页面标签jstl,后来被jsp2.0 ...
随机推荐
- ajax数据显示,使用js通用模板
最近用ajax获取数据,上级要求要自己写一个js模板,以往看到的js模板,大都数都是在js里面拼接的,现在换一种比较简单的写法, 通过ajax获取数据源,js模板循环显示数据 function Get ...
- oracle中job定时调用存储过程的实例
使用job模拟定时从元数据表中抽取指定数据进入目标表的过程. 一.创建元数据表 --create table test_origianl create table test_original_data ...
- Android Broadcast Receiver注册
之前有关 Broadcast Receiver的链 动态注册 1.我们新建一个广播接受类,我们创建一个内部类让他继承BroadcastReceiver,并且重新其中当有广播来到时执行的方法onRece ...
- atoi atol strtod strtol strtoul _gcvt
如果以下函数,您在使用的时候,总是输出一个莫名的值,是因为您忘记了引用头文件 #include <stdlib.h> 1- atoi int atoi(const char *nptr); ...
- 注册表添加python
win(python2.7)下: 执行此文件 #!/usr/bin/env python # encoding:utf-8 # # script to register Python 2.0 or l ...
- Ubuntu 14.04卸载安装失败的Mysql数据库,以及重新安装配置
一.删除原来Mysql 1.删除mysql的数据文件 sudo rm /var/lib/mysql/ -R 2.删除mqsql的配置文件 sudo rm /etc/mysql/ -R 3.自动卸载my ...
- AprioriTID algorithm
What is AprioriTID? AprioriTID is an algorithm for discovering frequent itemsets (groups of items ap ...
- Jar
http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#Intro http://docs.oracle.com/java ...
- 在类似qq或者微信聊天中。如何根据不同的手机发送图片
原文:在类似qq或者微信聊天中.如何根据不同的手机发送图片 前一段时间,公司自己要求做多客服开发,但是对于发送图片这一块,当时很苦恼,我用自己的手机(米2)测试,不管是本地,还是云相册,最新照片. ...
- Linux下chkconfig命令详解(转)
Linux下chkconfig命令详解 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. ...