java JSP 标记文件能否访问其调用 JSP 的 PageContext?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7098141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:37:08  来源:igfitidea点击:

Can a JSP tag file access its calling JSP's PageContext?

javajspeljsp-tags

提问by machineghost

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).

在 myTag.tag 里面......但我当然不能,因为标签文件无权访问 pageContext(相反,它可以访问 jspContext ......它与调用页面的属性不同页上下文)。

Now, you can access the pageContext via ELScript:

现在,您可以通过 ELScript 访问 pageContext:

${pageContext}

but that doesn't help because ELScript has no way of passing arguments, so you can't do:

但这无济于事,因为 ELScript 无法传递参数,所以你不能这样做:

${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.

然而,ELscript 可以访问页面上下文的事实,以及标签可以访问像 jspContext 这样的各种变量的事实,标签必须有某种方式来访问(以 scriptlet/Java 逻辑方式,而不仅仅是在ELScript) 来自调用 JSP 的 pageContext 的属性。

Is there?

在那儿?

采纳答案by BalusC

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.

至于 EL,${pageContext.getAttribute("foo")}仅适用于 EL 2.2。在此之前,正确的语法是${pageContext.foo}或只是${foo}. 另请参阅我们的EL 维基页面

However, the ${pageContext}isn't shared between the parent JSP file and the JSP tag. Each has its own instance.

但是,${pageContext}父 JSP 文件和 JSP 标记之间不共享。每个都有自己的实例。

You could eitherset it as request attribute instead:

您可以将设置为请求属性:

<% request.setAttribute("foo", "bar") %>
<custom:myTag />

with in the tag

在标签中

<%= request.getAttribute("foo") %>

or, with EL

或者,使用 EL

${requestScope.foo}

or

或者

${foo}

Or, better, you could pass it as a fullworthy tag attribute

或者,更好的是,您可以将其作为完整的标签属性传递

<custom:myTag foo="bar" />

with in the tag

在标签中

<%@attribute name="foo" required="true" %>
${pageContext.foo}

or just

要不就

<%@attribute name="foo" required="true" %>
${foo}

回答by mgaert

Looks like in WebLogic 10 at least, the implicit "application" object isavailable 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.

像在WebLogic中10看到至少,隐含的“应用程序”对象在标记文件可用,并且是的instanceof ServletContext的。也许使用它,当它确实是一个 ServletContext 之后,而不一定是更高级别的 pageContext。