转载自:http://www.cnblogs.com/tanglin_boy/archive/2010/01/18/1650871.html感谢原文作者的总结

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的上下文对象。

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);
细心的朋友可以发现这里的session是个Map对象, 在Struts2中底层的session都被封装成了Map类型. 我们可以直接操作这个Map对象进行对session的写入和读取操作, 而不用去直接操作HttpSession对象.

方法二:使用org.apache.struts2.ServletActionContext类

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;
    }
    
    //其他代码片段
}

(2)IoC方式(即使用Struts2 Aware拦截器)

要使用IoC方式,我们首先要告诉IoC容器(Container)想取得某个对象的意愿,通过实现相应的接口做到这点。

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;
    }
}

Struts2---ActionContext和ServletActionContext小结的更多相关文章

  1. ActionContext和ServletActionContext小结(转)

    ActionContext和ServletActionContext小结 1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Act ...

  2. ActionContext和ServletActionContext小结

    1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...

  3. 【struts2】ActionContext与ServletActionContext

    1 再探ActionContext 我们知道,ActionContext是Action执行时的上下文,里面存放着Action在执行时需要用到的对象,也称之为广义值栈.Struts2在每次执行Actio ...

  4. Struts2中ActionContext及ServletActionContext介绍(转载)

    1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...

  5. ActionContext和ServletActionContext区别

    1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...

  6. Struts中ActionContext和ServletActionContext的比较

    一.ActionContext在Struts2开发中除了将请求参数自动设置到Action的字段中,往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直 ...

  7. (转)ActionContext和ServletActionContext

    前面已经了解到ActionContext是Action执行时的上下文,里面存放着Action在执行时需要用到的对象,我们也称之为广义值栈. Struts2在每次执行Action之前都会创建新的Acti ...

  8. ActionContext 与 ServletActionContext获取Session的异同

    1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...

  9. Struts2中ActionContext和ServletActionContext

    转自:http://blog.sina.com.cn/s/blog_6c9bac050100y9iw.html 在Web应用程序开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在A ...

随机推荐

  1. SpringCloud微服务基础 Eureka、Feign、Ribbon、Zuul、Hystrix、配置中心的基础使用

    1.单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N ...

  2. Redhat 6.4 linux系统不重启识别热添加的硬盘方法

    1.1    选择虚拟机添加一块硬盘 1.2    查看系统当前磁盘信息 [root@zhongyi-test ~]# ls -l /dev/sd* brw-rw----. 1 root disk 8 ...

  3. Java数据处理

    对于形如“(TYPE=SITA##)&&(((CTYP=FPL##)||(CTYP=CHG##)||(CTYP=CNL##)||(CTYP=DLA##)||(CTYP=DL##)||( ...

  4. JAVAOOP1

    封装属性:alt+shift+s------r------alt+a-----回车 创建无参构造和带参构造:alt+shift+s----点击鼠标选择 格式化代码:ctrl+shift+f 成员变量系 ...

  5. PHP 多参数方法的重构

    假设我们要完成一个保存文章的功能,如果采用函数编程的方式,大概会是下面这个样子: <?php function saveArticle($title, $content, $categoryId ...

  6. CSS选取指定位置标签first-child、last-child、nth-child

    1.first-child 选择列表中的第一个标签. 2.last-child 选择列表中的最后一个标签 3.nth-child(n) 选择列表中的第n个标签 4.nth-child(2n) 选择列表 ...

  7. python入门基本知识

    1. 什么是语言 语言是一个事物与另外一个事物沟通的介质. python则是人(程序员)与计算机沟通的介质. 2. 什么是编程 编程就是程序员将自己想要让计算机做的事情用编程语言翻译出来写到一系列的文 ...

  8. 尺取法 poj 2566

    尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...

  9. urllib使用三--urlretrieve下载文件

    下载文件 urllib.urlretrieve() 参数: url:远程地址 filename:要保存到本地的文件 reporthook:下载状态报告 data:有就变成POST请求,有格式要求 返回 ...

  10. Sql Server多种分页性能的比较

    一.前言 因为工作关系,遇到了非常大的数据量的分页问题,数据总共有8000万吧,这个显然不是简单的分页能够解决的,需要从多多方面考虑,从分表.分库等等.但是这个也让我考虑到了分页性能的问题,在不同数据 ...