標籤:strutsrequestwrapper struts2
StrutsRequestWrapper的源碼// 核心代碼:public class StrutsRequestWrapper extends HttpServletRequestWrapper { /** * 注意下面這句注釋:擷取object,如果沒找到就去ValueStack裡面找 * 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")) { return super.getAttribute(s); } ActionContext ctx = ActionContext.getContext();// ***** 調用父類的getAttribute()方法,擷取request範圍中的屬性 ***** 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 {// ***** 如果在request中沒找到,那就去ValueStack中找 ***** // If not found, then try the ValueStack ctx.put("__requestWrapper.getAttribute", Boolean.TRUE); ValueStack stack = ctx.getValueStack(); if (stack != null) {// ***** 調用findValue()方法,在ValueStack中找[先從對象棧(Value Stack Contents)中找,若沒找到,就去map棧(Stack Context)中找] attribute = stack.findValue(s); } } finally { ctx.put("__requestWrapper.getAttribute", Boolean.FALSE); } } } } return attribute; }}所以:在jsp頁面中使用EL運算式也可以訪問到struts2中ValueStack裡面的內容EL運算式的尋找順序:page --> request --> valueStack.findValue() --> session --> application
Struts2源碼分析(一) 一一一 StrutsRequestWrapper