Struts2中EL表達式的取值順序及OGNL表達式的取值順序

好記性不如賴筆頭…………

正常EL的查找域爲:page(PageContext)–>request–>session–>application

Struts2中EL的查找域爲:page(PageContext)–>request–>contextMap–>ValueStack–>session–>application

有的人說Struts2中EL的取值順序是:page(PageContext)–>request–>ValueStack–>contextMap–>session–>application,
但查看源碼(org.apache.struts2.dispatcher.StrutsRequestWrapper)發現request中的取值順序如下:

 public Object getAttribute(String key) {
        if (key == null) {
            throw new NullPointerException("You must specify a key value");
        }

        if (disableRequestAttributeValueStackLookup || key.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(key);
        }

        **//注意,這裏是先查找的actionContext中的contextMap的內容**
        ActionContext ctx = ActionContext.getContext();
        Object attribute = super.getAttribute(key);

        if (ctx != null && attribute == null) {
            boolean alreadyIn = isTrue((Boolean) ctx.get(REQUEST_WRAPPER_GET_ATTRIBUTE));

            // note: we don't let # come through or else a request for
            // #attr.foo or #request.foo could cause an endless loop
            if (!alreadyIn && !key.contains("#")) {
                try {
                    // If not found, then try the ValueStack
                    ctx.put(REQUEST_WRAPPER_GET_ATTRIBUTE, Boolean.TRUE);

                    **//最後,如果request、contextMap都沒有,纔去的valueStacK去查找**
                    ValueStack stack = ctx.getValueStack();
                    if (stack != null) {
                        attribute = stack.findValue(key);
                    }
                } finally {
                    ctx.put(REQUEST_WRAPPER_GET_ATTRIBUTE, Boolean.FALSE);
                }
            }
        }
        return attribute;
    }

OGNL的查找域爲:page(PageContext)–>ValueStack–>contextMap–>request–>session–>application

發佈了153 篇原創文章 · 獲贊 69 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章