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

How to access var variable in foreach tag in custom tag?

如何访问自定义标签中foreach标签的​​var值?

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

如何将 var 的值分配给自定义标记中的属性名称?

我用了pageContext.getAttribute(\"item"\),但没用。

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