`
Flemming323
  • 浏览: 9644 次
  • 性别: Icon_minigender_1
  • 来自: 长春
社区版块
存档分类
最新评论

ActionContext和ServletActionContext小结----摘抄自‘挨踢民工’

    博客分类:
  • Java
 
阅读更多

ActionContext和ServletActionContext小结----摘抄自‘挨踢民工’

 http://www.cnblogs.com/tanglin_boy/category/227727.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;
    }
}

分享到:
评论

相关推荐

    strut2 详解

    struts2.0 详细介绍了struts.xml的配置,以及web.xml的配置和ActionContext,ServletActionContext的用法和区别。ActionContext主要用于设置属性,而ServletActionContext主要用来得到属性

    struts_2.3.12GA_API文档(chm版本)

    -------------------------------------------------------------------------------- 概述 软件包 类 使用 树 已过时 索引 帮助 上一个类 下一个类 框架 无框架 所有类 摘要: 嵌套 | 字段 | 构造方法 | 方法 ...

    使用Action访问ActionContext方式的网站计数器

    使用Action访问ActionContext方式的网站计数器,可直接运行

    struts2文件上传,与struts2-xml校验

    System.out.println("上下文路径="+ ActionContext.getContext()); System.out.println("aaa="+ServletActionContext.getActionContext(ServletActionContext.getRequest())); filename=new String[myFile....

    Struts2通过使用ActionContext类获取request和response对象

    NULL 博文链接:https://zhouhaitao.iteye.com/blog/1126042

    ActionContext介绍(在Struts2中)

    在Web应用程序开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话 (Session)的一些信息, 甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应...

    struts2中的ActionContext与ognl

    NULL 博文链接:https://lijiejava.iteye.com/blog/628636

    JavaEE ActionContext存取数据示例

    JavaEE ActionContext存取数据示例

    基于struts2注册登录ActionContext.zip

    struts2大量使用拦截器来处理请求,从而允许与业务逻辑控制器 与 servlet-api分离,避免了侵入性(所谓侵入性就是指的这个架构设计出来的部件对系统的影响范围,标签库几乎可以完全替代JSTL的标签库,并且 struts2.x...

    ActionContext在struts2.0中的详细应用

    ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放放的是Action在执行时需要用到的对象

    Struts2_TypeConvertion

    这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及org.apache.struts2.ServletActionContext类,具体的方法如下所示。 获得request对象: A . HttpServletRequest request = ServletActionContext....

    struts2OGNL表达式ActionContext及valuesStack.pdf

    struts2OGNL表达式ActionContext及valuesStack.pdf

    xwork-core-2.3.37.jar

    用于解决struts2升级至2.3.37时 出现java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.put(Ljava/lang/异常时,更换的xwork-core-2.3.37.jar

    Action获取Web元素

    Action获取Web元素: 1通过ActionContext获取 2通过ServletActionContext获取 3框架注入

    Struts2 in action中文版

    6.1.1 ActionContext和OGNL 109 6.1.2 虚拟对象ValueStack 111 6.2 Struts 2标签概要 113 6.2.1 Struts 2标签API语法 113 6.2.2 使用OGNL设置标签属性 115 6.3 数据标签 117 6.3.1 property标签 117 6.3.2 set标签 ...

    struts2_OGNL表达式ActionContext及valuesStack

    struts2 OGNL,struts2 表达式语言,Struts2 中OGNL表达式的用法,Struts2 #,表达式语言的好处,Struts2 $,struts2 井号,星号,百分号

    struts2 标签库 帮助文档

    可以输出值栈、StackContext 、ActionContext中的值。 push:将某个值放入值栈。 set:设置一个新的变量。 text:输出国际化信息。 url:生成一个URL地址。 表单标签: checkbox标签:复选框。 checkboxlist标签...

    struts2-junit-plugin-2.1.8.jar

    可解决java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.get(Ljava/lang/Object;)Ljava/lang/Object; java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.TextUtils struts2.1.8...

    Struts2实战总结

    3:在action类中取得request和session对象的方法 Map session = ActionContext.getContext().getSession(); HttpServletRequest request = ServletActionContext.getRequest (); 设置它们的值的方法

    Struts2 ActionContext 中的数据详解

    主要介绍了Struts2 ActionContext 中的数据详解,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics