将c:forEach变量传递给jsp:include

lit*_*ahn 17 foreach jsp scope jstl jspinclude

我试图访问一些在包含JSTL for循环中设置的JSTL变量.

例:

<c:forEach items="${cart.entries}" var="entry">
 <jsp:include page="helper.jsp"></jsp:include>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

在helper.jsp里面我希望能够引用变量'entry'.它一直是'空'.我想也许可能有一种方法可以将范围添加到forEach变量,就像使用普通的设置变量一样.

有任何想法吗?

lit*_*ahn 28

答案:我最终不得不这样做才能让它发挥作用.

<c:forEach items="${cart.entries}" var="entry">
<c:set var="entryFC" value="${entry}" scope="request"></c:set>
 <jsp:include page="helper.jsp"></jsp:include>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

然后我在我的include中引用了entryFC.不是很优雅,但它的工作,所以我想生病了.

  • 我正在寻找一种更优雅的方式,但最终做到了这一点.顺便说一下,你不需要重命名变量:`<c:set var ="entry"value ="$ {entry}"scope ="request"/>`工作正常,可能不那么令人困惑. (12认同)