Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

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

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"/>

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>

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")); %>

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.

How do I fix this?


*(yes, I know, scriplets==bad, this is just for debugging this one problem)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

<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. This may end up to be more useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...