JSP 标记文件可以访问其调用 JSP 的 PageContext 吗?
如果我这样做:
<% pageContext.setAttribute("foo", "bar"); %>
<custom:myTag/>
似乎我应该能够这样做:
<%= pageContext.getAttribute("foo") %>
在 myTag.tag 内部......但我当然不能,因为标记文件无权访问 pageContext (相反,它可以访问 jspContext 。 .. 与调用页面的 pageContext 不具有相同的属性)。
现在,您可以通过 ELScript 访问 pageContext:
${pageContext}
但这没有帮助,因为 ELScript 无法传递参数,所以您不能这样做:
${pageContext.getAttribute("foo")}
但是,ELscript 可以访问页面上下文,并且标记可以访问各种变量,如 jspContext,标签必须有某种方式来访问(以 scriptlet/Java 逻辑方式,而不仅仅是 ELScript)来自调用 JSP 的 pageContext 的属性。
有没有?
If I do:
<% pageContext.setAttribute("foo", "bar"); %>
<custom:myTag/>
it seems like I should be able to do:
<%= pageContext.getAttribute("foo") %>
inside of myTag.tag ... but of course I can't because the tag file doesn't have access to the pageContext (instead it has access to a jspContext ... which doesn't have the same attributes as the calling page's pageContext).
Now, you can access the pageContext via ELScript:
${pageContext}
but that doesn't help because ELScript has no way of passing arguments, so you can't do:
${pageContext.getAttribute("foo")}
However, the fact that ELscript can accesss the page context, and the fact that the tag can access all sorts of variables like jspContext, that there must be some way for a tag to access (in a scriptlet/Java logic way, not just in ELScript) an attribute from the calling JSP's pageContext.
Is there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 EL,
${pageContext.getAttribute("foo")}
仅适用于 EL 2.2。在此之前,正确的语法是${pageContext.foo}
或只是${foo}
。另请参阅我们的 EL wiki 页面。但是,
${pageContext}
不在父 JSP 文件和 JSP 标记之间共享。每个都有自己的实例。您可以将其设置为请求属性:
在标签中使用
,或者使用 EL
或
或者,更好的是,您可以将其作为完整的标签属性
在标签中
传递或者只是
As to EL, the
${pageContext.getAttribute("foo")}
works in EL 2.2 only. Before that the right syntax is${pageContext.foo}
or just${foo}
. See also our EL wiki page.However, the
${pageContext}
isn't shared between the parent JSP file and the JSP tag. Each has its own instance.You could either set it as request attribute instead:
with in the tag
or, with EL
or
Or, better, you could pass it as a fullworthy tag attribute
with in the tag
or just
至少在 WebLogic 10 中,隐式“应用程序”对象在标记文件中可用,并且是 ServletContext 的实例。也许可以使用这个,当它实际上是 ServletContext 之后,而不一定是更高级别的 pageContext。
Looks like in WebLogic 10 at least, the implicit "application" object is available in tag files, and is instanceof ServletContext. Maybe use this, when it's really the ServletContext that one is after, and not necessarily the higher-level pageContext.