1. 程式人生 > >Struts2中EL表示式的取值順序及OGNL表示式的取值順序

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