如何通过跨上下文JSTL导入将参数传递给JSP? [英] How do I pass a parameter to a JSP via a cross-context JSTL import?
问题描述
我遇到了其他一些问题,它们描述了与我相似但不完全相同的情况.例如,这个问题显示了几乎相同的问题,除了我没有使用Portlet-我只是在使用无聊的JSP + JSTL + EL + etc.
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.
我有两个应用程序上下文,并且我想将JSP从一个导入到另一个.我知道该怎么做:
I have two application contexts, and I'd like to import a JSP from one to the other. I know how do that:
<c:import context="/" url="/WEB-INF/jsp/foo.jsp"/>
但是,我也想将参数传递给导入的foo.jsp
.但是这段代码:
However, I also want to pass a parameter to the imported foo.jsp
. But this code:
<c:import context="/" url="/WEB-INF/jsp/foo.jsp">
<c:param name="someAttr" value="someValue"/>
</c:import>
似乎没有正确将参数发送到foo.jsp
;如果foo.jsp
类似于*
does not seem to properly send the parameter to foo.jsp
; if foo.jsp
is something like*
<% System.out.println("foo.jsp sees that someAttr is: "
+ pageContext.findAttribute("someAttr")); %>
然后将其打印出来:
foo.jsp sees that someAttr is: null
我想看这个
foo.jsp sees that someAttr is: someValue
因此,很明显,在foo.jsp
中找不到someAttr
.
so, obviously, someAttr
can't be found in foo.jsp
.
我该如何解决?
*(是的,我知道,scriplets==bad
,这只是用于调试这一问题)
*(yes, I know, scriplets==bad
, this is just for debugging this one problem)
推荐答案
您正在将其设置为请求参数,因此也应该将其作为请求参数.
You're setting it as a request parameter, so you should also be getting it as request parameter.
由于您似乎也不喜欢scriptlet,因此这是一种EL解决方案:
Since you seem to dislike scriptlets as well, here's an EL solution:
${param.someAttr}
请注意,在这种特殊情况下,<c:import>
不会在<jsp:include>
之上添加任何其他优势.每当您要从其他上下文或完全不同的域导入文件时,此功能都非常有用,但是现在似乎并非如此.以下内容也应该已经起作用:
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:
<jsp:include page="/WEB-INF/jsp/foo.jsp">
<jsp:param name="someAttr" value="someValue" />
</jsp:include>
这样,包含的页面可以访问与主JSP相同的PageContext
和 HttpServletRequest
.最终可能会更有用.
This way the included page has access to the same PageContext
and HttpServletRequest
as the main JSP. This may end up to be more useful.
这篇关于如何通过跨上下文JSTL导入将参数传递给JSP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!