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

你可能感兴趣的:(struts2)