Ordinary hut

          人間一福地,勝似天仙宮
          posts - 61, comments - 50, trackbacks - 0, articles - 1

          關于struts2的ActionContext和ValueStack的簡單理解

          Posted on 2009-04-07 20:47 landor 閱讀(3676) 評論(0)  編輯  收藏 所屬分類: struts2
          ActionContext是被存放在當前線程中的,獲取ActionContext也是從ThreadLocal中獲取的。所以在執行攔截器、 action和result的過程中,由于他們都是在一個線程中按照順序執行的,所以可以可以在任意時候在ThreadLocal中獲取 ActionContext。
          ActionContext包括了很多信息,比如Session、Application、Request、Locale、ValueStack等,其中 ValueStack可以解析ognl表達式,來動態后去一些值,同時可以給表達式提供對象。值棧是建立在ognl的基礎之上的。

          HttpServletRequest等對象是如何與struts2的ActionContext互通的
          是struts2重新實現了HttpServletRequest接口,就是類StrutsRequestWrapper,看下struts2的說明:
          All Struts requests are wrapped with this class, which provides simple JSTL accessibility. This is because JSTL works with request attributes, so this class delegates to the value stack except for a few cases where required to prevent infinite loops. Namely, we don't let any attribute name with "#" in it delegate out to the value stack, as it could potentially cause an infinite loop. For example, an infinite loop would take place if you called: request.getAttribute("#attr.foo").
          說明struts重新包裝了request接口,所以如果用request.getAttribute()的話,是執行了StrutsRequestWrapper類,在這個類里面有訪問ActionContext的代碼:
          public class StrutsRequestWrapper extends HttpServletRequestWrapper {

              
          /**
               * The constructor
               * 
          @param req The request
               
          */
              
          public StrutsRequestWrapper(HttpServletRequest req) {
                  
          super(req);
              }

              
          /**
               * Gets the object, looking in the value stack if not found
               *
               * 
          @param s The attribute key
               
          */
              
          public Object getAttribute(String s) {
                  
          if (s != null && s.startsWith("javax.servlet")) {
                      
          // don't bother with the standard javax.servlet attributes, we can short-circuit this
                      
          // see WW-953 and the forums post linked in that issue for more info
                      return super.getAttribute(s);
                  }

                  ActionContext ctx 
          = ActionContext.getContext();
                  Object attribute 
          = super.getAttribute(s);
                  

          在這里實現了getAttribute方法,然而沒有實現全部,所以只有用getAttribute方法才能獲取ActionContext中的信息
          但是它繼承了sun的HttpServletRequestWrapper類,所以其他的方法還是用的默認實現,也就是不能訪問到ActionContext中的信息
          jstl標簽會觸發request等的getAttribute方法,所以jstl也能獲取到ActionContext的數據,比如Struts2的Action中的屬性等等。
          主站蜘蛛池模板: 武城县| 科技| 枣强县| 获嘉县| 周口市| 二手房| 商城县| 策勒县| 图们市| 江津市| 阳城县| 霞浦县| 商城县| 永福县| 文山县| 台湾省| 定兴县| 左权县| 根河市| 五常市| 鄂托克前旗| 临潭县| 乌苏市| 绥江县| 犍为县| 大埔县| 兴宁市| 太仓市| 阳原县| 丹寨县| 商丘市| 高唐县| 云林县| 揭东县| 吐鲁番市| 南康市| 潮州市| 凯里市| 策勒县| 右玉县| 太仓市|