zoukankan      html  css  js  c++  java
  • strutrs contextMap

    contextMap(非常重要)

          1、动作类的生命周期

                                明确:动作类是多例的,每次动作访问,动作类都会实例化。所以是线程安全的。与Struts1的区别是,struts1的动作类是单例的。

          2、请求动作的数据存放

                        问题:

                         每次请求时,都会产生一些请求数据,这些数据存放到哪里去了?

                   明确:

            在每次动作执行前,核心控制器StrutsPrepareAndExecuteFilter都会创建一个ActionContextValueStack对象。且每次动作访问都会创建。

            这两个对象存储了整个动作访问期间用到的数据。并且把数据绑定到了线程局部变量(ThreadLocal)上了。所以是线程安全的。

             struts2的核心控制器的 doFilter 方法

     1   public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
     2 
     3         HttpServletRequest request = (HttpServletRequest) req;
     4         HttpServletResponse response = (HttpServletResponse) res;
     5 
     6         try {
     7             prepare.setEncodingAndLocale(request, response);
     8             prepare.createActionContext(request, response);
     9             prepare.assignDispatcherToThread();
    10             if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
    11                 chain.doFilter(request, response);
    12             } else {
    13                 request = prepare.wrapRequest(request);
    14                 ActionMapping mapping = prepare.findActionMapping(request, response, true);
    15                 if (mapping == null) {
    16                     boolean handled = execute.executeStaticResourceRequest(request, response);
    17                     if (!handled) {
    18                         chain.doFilter(request, response);
    19                     }
    20                 } else {
    21                     execute.executeAction(request, response, mapping);
    22                 }
    23             }
    24         } finally {
    25             prepare.cleanupRequest(request);
    26         }
    27     }
    doFilter

               ActionContext 的部分代码

     1 public class ActionContext implements Serializable {
     2 
     3     static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();
     4 
     5     /**
     6      * Constant for the name of the action being executed.
     7      */
     8     public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";
     9 
    10     /**
    11      * Constant for the {@link com.opensymphony.xwork2.util.ValueStack OGNL value stack}.
    12      */
    13     public static final String VALUE_STACK = ValueStack.VALUE_STACK;
    14 
    15     /**
    16      * Constant for the action's session.
    17      */
    18     public static final String SESSION = "com.opensymphony.xwork2.ActionContext.session";
    ActionContext

          3、contextMap:存储数据

                                Struts2的官方文档对contextMap的说明:

                                            

     

    contextMap中存放的主要内容

    Key

    Value

    说明

    value stack (root)

    java.util.List

    没有root这个key。它是一个list。

    application

    java.util.Map<String,Object>

    ServletContext中的所有属性。

    session

    java.util.Map<String,Object>

    HttpSession中的所有属性。

    request

    java.util.Map<String,Object>

    ServletRequest中的所有属性。

    parameters

    java.util.Map

    参数

    attr

    java.util.Map

    把页面、请求、会话、应用范围内的所有属性放到一起。

                                                   注意:

                                                            除了value stack之外,全是map,而contextMap也是一个map。其实就是Map中又封装的Map。(很像dbutils中KeyedHandler封装数据的结构,只是封装数据的结构)

                                       查看contextMap中的数据:

                                                     在页面上使用<s:debug/>

              

  • 相关阅读:
    jdk8u144安装在centos7上
    事件对象(示例、封装函数EventUtil())
    事件处理程序
    取消冒泡
    编程挑战JavaScript进阶篇(慕课网题目)
    网页卷去的距离与偏移量
    DOM编程练习(慕课网题目)
    浏览器窗口可视区域大小
    网页尺寸scrollHeight/offsetHeight
    替换元素节点replaceChild()
  • 原文地址:https://www.cnblogs.com/soficircle/p/7062266.html
Copyright © 2011-2022 走看看