2014年1月7日星期二

EL expressions [ new ] Why can salvage value in the value stack

 This post last edited by the iwieijakdf on 2014-01-07 08:16:12
I have a list value of a collection of objects into a stack Map stack in the background

public String getLastVersionPD(){
ActionContext.getContext().put("pdList", this.pdManager.getLastVersonPD());
return "listAction";
}

then iteration OGNL expressions in the foreground

<s:iterator value="#pdList">
  <s:property value="name"/>
  <s:property value="version"/>
  <s:a action="pdManagerAction_delete?key=%{key}">删除</s:a>
  <a href="javascript: showProcessImage('${requestScope.deploymentId}')">查看流程图</a>
</s:iterator>

name version key deploymentId is a property of an object , the value is placed on top of the stack when the stack object iteration , in front of the name version key expression can use OGNL value stack objects in the stack take the value , why use EL expressions can also be obtained at the request scoped attribute deploymentId it
------ Solution --------------------- -----------------------
landlord this issue , why take the request scope with el things will also take it to the value of the contents of the stack . problem it is necessary to combine struts2 source , we know there is HttpServletRequest request in javaee it is all packaged inside an interface is implemented by javax.servlet.http.HttpServletRequestWrapper class . struts2 own javax.servlet.http . HttpServletRequestWrapper achieve , that is org.apache.struts2.dispatcher.StrutsRequestWrapper in struts2-core packet re getAttribute method was rewritten.
We know the value of request scope el expressions take is to call request.getAttribute (String s);
what this is struts2 getAttribute method of org.apache.struts2.dispatcher.StrutsRequestWrapper rewriting the source code for part

public Object getAttribute(String s) {
        if (s != null && s.startsWith("javax.servlet")) {
            //如果是${requestScope.javax.servlet.属性名的}就直接去request里面取值
            return super.getAttribute(s);
        }
//关键在下面,先从request里面取值取不到时就从值栈里面取值,所以就可以解释楼主这个为啥也能取到值了
        ActionContext ctx = ActionContext.getContext();
        Object attribute = super.getAttribute(s);
        if (ctx != null) {
            if (attribute == null) {
                boolean alreadyIn = false;
                Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute");
                if (b != null) {
                    alreadyIn = b.booleanValue();
                }
                if (!alreadyIn && s.indexOf("#") == -1) {
                    try {
                        // If not found, then try the ValueStack
                        ctx.put("__requestWrapper.getAttribute", Boolean.TRUE);
                        ValueStack stack = ctx.getValueStack();
                        if (stack != null) {
                            attribute = stack.findValue(s);
                        }
                    } finally {
                        ctx.put("__requestWrapper.getAttribute", Boolean.FALSE);
                    }
                }
            }
        }
        return attribute;
    }

------ For reference only ----------------------------------- ----
# non- root object is accessed , it is non- value data stack , such as ognl context and action context. # Can be understood as ActionContext.getContext ().
EL expressions, such as $ {username}, equivalent to request.getParameter (username), and then you go to understand what is meant by request it.
------ For reference only -------------------------------------- -
<s:iterator value="#pdList">
this sentence is to traverse the collection every single element in turn placed on the stack , el expression values ​​also directly from the value stack , so we get to the amount
--- --- For reference only ---------------------------------------
understand Thank

没有评论:

发表评论