[英]Can a JSP tag file access its calling JSP's PageContext?
如果我做:
<% pageContext.setAttribute("foo", "bar"); %>
<custom:myTag/>
看來我應該能夠做到:
<%= pageContext.getAttribute("foo") %>
myTag.tag 內部...但我當然不能,因為標記文件無權訪問 pageContext(相反,它可以訪問 jspContext...,它與調用頁面的屬性不同頁面上下文)。
現在,您可以通過 ELScript 訪問 pageContext:
${pageContext}
但這無濟於事,因為 ELScript 無法通過 arguments,所以你不能這樣做:
${pageContext.getAttribute("foo")}
然而,事實上 ELscript 可以訪問頁面上下文,以及標簽可以訪問各種變量(如 jspContext)的事實,標簽必須有某種方式來訪問(以 scriptlet/Java 邏輯方式,而不僅僅是在ELScript) 來自調用 JSP 的 pageContext 的屬性。
在那兒?
至於 EL, ${pageContext.getAttribute("foo")}
僅適用於 EL 2.2。 在此之前,正確的語法是${pageContext.foo}
或只是${foo}
。 另請參閱我們的EL wiki 頁面。
但是, ${pageContext}
不會在父 JSP 文件和 JSP 標記之間共享。 每個都有自己的實例。
您可以將其設置為請求屬性:
<% request.setAttribute("foo", "bar") %>
<custom:myTag />
在標簽中
<%= request.getAttribute("foo") %>
或者,使用 EL
${requestScope.foo}
或者
${foo}
或者,更好的是,您可以將它作為一個完整的標簽屬性傳遞
<custom:myTag foo="bar" />
在標簽中
<%@attribute name="foo" required="true" %>
${pageContext.foo}
要不就
<%@attribute name="foo" required="true" %>
${foo}
看起來至少在 WebLogic 10 中,隱式“應用程序”object 在標記文件中可用,並且是 ServletContext 的實例。 也許使用這個,當它確實是一個所追求的ServletContext,而不一定是更高級別的pageContext。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.