简体   繁体   English

如何通过跨上下文 JSTL 导入将参数传递给 JSP?

[英]How do I pass a parameter to a JSP via a cross-context JSTL import?

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.我有两个应用程序上下文,我想将 JSP 从一个导入到另一个。 I know how do that:我知道怎么做:

<c:import context="/" url="/WEB-INF/jsp/foo.jsp"/>

However, I also want to pass a parameter to the imported foo.jsp .但是,我还想将参数传递给导入的foo.jsp But this code:但是这段代码:

<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 ;似乎没有正确地将参数发送到foo.jsp if foo.jsp is something like*如果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 .所以,很明显,在foo.jsp找不到someAttr

How do I fix this?我该如何解决这个问题?


*(yes, I know, scriplets==bad , this is just for debugging this one problem) *(是的,我知道, scriplets==bad ,这只是为了调试这个问题)

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.请注意,在这种特殊情况下, <c:import>没有在<jsp:include>之上添加任何额外的优势。 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>

This way the included page has access to the same PageContext and HttpServletRequest as the main JSP.这样,包含的页面可以访问与主 JSP 相同的PageContextHttpServletRequest This may end up to be more useful.这最终可能会更有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM