zoukankan      html  css  js  c++  java
  • struts2里面session设置 程序员

    Struts2中Action的execute方法是没有任何的参数。

    哪我们如何去获得作用域对象,没有作用域对象就没有办法操作里面的数据。

    到这里我们先不要着急,首先我们来讲解一下Struts2中Action的运行过程。

    当Struts2的过滤器启动的时候,首先就会初始化一个叫做ServletActionContext的类。

    它掌管着所有的作用域对象。

    import org.apache.struts2.ServletActionContext;

    public class ContextAction implements Action {

    public String execute() throws Exception {


    //获得请求
    HttpServletRequest _request = ServletActionContext.getRequest();


    //获得会话
    HttpSession _session = _request.getSession();


    //获得上下文对象
    ServletContext _application = ServletActionContext.getServletContext();
    }

    }
    -------------------------------------------------------------------------------

    所以一个普通的Action类就可以通过这种方式得到所有的作用域对象。

    当然这是Strtus2中才能使用的特性。

    -------------------------------------------------------------------------------

    而在原来的webwork中又是如何得到作用域对象的?

    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;

    public class ContextAction implements Action {

    public String execute() throws Exception {

    ActionContext ctx = ActionContext.getContext();//得到Struts2的上下文


    Map request = (Map)ctx.get("request");//得到request


    Map session = ctx.getSession();//得到会话


    Map application = ctx.getApplication();//得到应用程序



    request.put("welcome", "欢迎进入Struts2的世界!");
    session.put("hello", "hi,banseon welcome!");

    Integer count = (Integer)application.get("count");
    if(null == count){
    count =1;
    }else
    count++;
    application.put("count", count);
    return SUCCESS;
    }
    }
    ------------------------------------------------------------------------------------------------

    想必看到这里大家可能就有点糊涂了。其实webwork框架把作用域对象封装到

    ActionContext中直接将request,session,Application还原成了Map集合并封装到

    该类中。并为此提供了request.getSession(),request.getApplication()等方法。

    如何我们使用其他比较方便。但是对于我们使用JAVAEE中request,session,application

    的习惯有很大的不同。所以Strtus2就在相应的jar中做出了调整所以ServletActionContext。

    就因此而来了。

    当然,这两种用法都是可以直接来操作作用域对象。

    怎么使用就看仁者见仁。

  • 相关阅读:
    mvn command is not recognized as an internal or external command
    mac搭建nginx和wordpress开发环境
    搭建nginx+php环境时遇到”file not found"的问题
    MAC下bash配置文件的加载顺序
    lsof的注意事项
    理解字符集、字符编码、ASCII、Unicode、UTF-8
    初学markdown有感
    AngularJS开发指南-Animations
    #正则表达式# 学习笔记
    vi学习笔记/基本操作方法
  • 原文地址:https://www.cnblogs.com/sallon/p/2616650.html
Copyright © 2011-2022 走看看