java 如何通过跨上下文 JSTL 导入将参数传递给 JSP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3920546/
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
How do I pass a parameter to a JSP via a cross-context JSTL import?
提问by Matt Ball
I've come across a few other questions that describe a similar, but not identical situation, to mine. This question, for instance, shows pretty much the same problem, except that I'm not using portlets - I'm just using boring ol' JSP+JSTL+EL+etc.
我遇到了一些其他问题,这些问题描述了与我类似但不完全相同的情况。例如,这个问题显示了几乎相同的问题,除了我没有使用 portlet - 我只是使用无聊的 ol' JSP+JSTL+EL+ 等。
I have two application contexts, and I'd like to import a JSP from one to the other. I know how do that:
我有两个应用程序上下文,我想将 JSP 从一个导入到另一个。我知道怎么做:
<c:import context="/" url="/WEB-INF/jsp/foo.jsp"/>
However, I also want to pass a parameter to the imported foo.jsp
. But this code:
但是,我还想将参数传递给导入的foo.jsp
. 但是这段代码:
<c:import context="/" url="/WEB-INF/jsp/foo.jsp">
<c:param name="someAttr" value="someValue"/>
</c:import>
does not seem to properly send the parameter to foo.jsp
; if foo.jsp
is something like*
似乎没有正确地将参数发送到foo.jsp
; 如果foo.jsp
是这样的*
<% System.out.println("foo.jsp sees that someAttr is: "
+ pageContext.findAttribute("someAttr")); %>
then this gets printed out:
然后打印出来:
foo.jsp sees that someAttr is: null
whereas I want to see this:
而我想看到这个:
foo.jsp sees that someAttr is: someValue
so, obviously, someAttr
can't be found in foo.jsp
.
所以,显然,someAttr
不能在foo.jsp
.
How do I fix this?
我该如何解决?
*(yes, I know, scriplets==bad
, this is just for debugging this one problem)
*(是的,我知道,scriplets==bad
这只是为了调试这个问题)
回答by BalusC
You're setting it as a request parameter, so you should also be getting it as request parameter.
您将其设置为请求参数,因此您也应该将其作为请求参数获取。
Since you seem to dislike scriptlets as well, here's an EL solution:
由于您似乎也不喜欢 scriptlet,这里有一个 EL 解决方案:
${param.someAttr}
Note that <c:import>
doesn't add any extra advantages above <jsp:include>
in this particular case. It's useful whenever you want to import files from a different context or an entirely different domain, but this doesn't seem to be the case now. The following should also just have worked:
请注意,在这种特殊情况下<c:import>
不会增加上述任何额外优势<jsp:include>
。当您想从不同的上下文或完全不同的域导入文件时,它很有用,但现在似乎并非如此。以下内容也应该有效:
<jsp:include page="/WEB-INF/jsp/foo.jsp">
<jsp:param name="someAttr" value="someValue" />
</jsp:include>
This way the included page has access to the same PageContext
andHttpServletRequest
as the main JSP. This may end up to be more useful.
通过这种方式,包括页面访问相同PageContext
,并HttpServletRequest
作为主JSP。这最终可能会更有用。