如何在自定义标记中访问foreach标记中的var变量?

时间:2018-04-09 00:11:04

标签: java jsp jstl

如何在自定义标记中访问foreach标记的var值?

<c:foreach items=“{collection}” var=“items”>
    <custom:tag name=items />
</foreach>

如何在自定义标记中为属性名称赋值var?

我使用了pageContext.getAttribute(\"item"\),但它无效。

1 个答案:

答案 0 :(得分:0)

<c:foreach items=“{collection}” var=“items”>
    <c:set var="itemsToPass" value="${items}"/>
    <% pageContext.setAttribute("forEachVar", itemsToPass); %>
    <custom:tag name="${pageScope.forEachVar}"/>
</c:forEach>

您可以在标记中使用${pageScope.forEachVar}(请记住将其转换为您需要的任何内容,例如使用pageContext.getAttribute(...)将对象转换为字符串,如String itemsName= (String) pageContext.getAttribute("forEachVar");)。

相关问题