将一组参数传递给jsp包括使用jstl

时间:2016-07-19 11:53:04

标签: java jsp jstl

我在这方面遇到了一些麻烦,谷歌对这个特定主题并没有什么帮助。

我想要执行以下内容:

<c:forEach var="block" items="${blocks}">
    <jsp:include page="${block.blockJSP}"/
</c:forEach>

背后的想法是我有一套&#34;块&#34;。这允许我以模块化方式创建页面。这很好用,我对它的工作方式感到满意。

现在我想通过在创建页面时向其传递几个键/值对来自定义块的内容:

<c:forEach var="block" items="${blocks}">
    <jsp:include page="${block.blockJSP}">
        <c:forEach var="blockParam" items="${block.blockParameters}">
            <jsp:param name="${blockParam.key}" value="${blockParam.value}"/>
        </c:forEach>
    </jsp:include>
</c:forEach>

然而,这给了我一个令人讨厌的JasperException:

org.apache.jasper.JasperException: /jsp/survey.jsp (line: 113, column: 24) Expecting "jsp:param" standard action with "name" and "value" attributes
        at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
        at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
        at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:89)
        at org.apache.jasper.compiler.Parser.parseParam(Parser.java:809)
        at org.apache.jasper.compiler.Parser.parseBody(Parser.java:1655)
        at org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:986)
        at org.apache.jasper.compiler.Parser.parseInclude(Parser.java:838)
        at org.apache.jasper.compiler.Parser.parseStandardAction(Parser.java:1100)
        at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1434)
        at org.apache.jasper.compiler.Parser.parseBody(Parser.java:1647)
        at org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:986)
        at org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1258)
        at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1435)
        at org.apache.jasper.compiler.Parser.parse(Parser.java:138)
        at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242)
        at org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
        at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
        at org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
        at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
        at com.narran.survey.SurveyServlet.showSurvey(SurveyServlet.java:45)
        at com.narran.survey.SurveyServlet.doGet(SurveyServlet.java:27)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)

也许我对jstl的有限理解阻止我解决这个问题。我是否正确设置了这个?

1 个答案:

答案 0 :(得分:1)

我不知道你的blockJSP是什么样的,但我建议这个解决方法:

<c:forEach var="block" items="${blocks}">
    <c:set var="blockParams" value="${block.blockParameters}" scope="request" />
    <jsp:include page="${block.blockJSP}" />
</c:forEach>

这将设置所有requestScope中可用的集合blockParams,因此它可以在与块对应的.jsp中访问 - 将此jsp中的集合视为变量而不是参数。

来源:Passing c:forEach variable to a jsp:include