java 嵌套 forEach 的 JSF JSTL 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/619097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSF JSTL problem with nested forEach
提问by noup
Inside a nested foreach, accessing the same variable is returning different values. This happens when the page is reloaded, not on first load.
在嵌套的 foreach 中,访问相同的变量会返回不同的值。这发生在页面重新加载时,而不是在第一次加载时。
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
(...)
xmlns:c="http://java.sun.com/jstl/core"
xmlns:h="http://java.sun.com/jsf/html">
(...)
<c:forEach items="#{controller.availableTransitions}" var="transition">
<c:forEach items="#{transition.availableTransitions}" var="transitionItem">
<h:outputText value="1_#{transitionItem.name} 2_#{transitionItem.name}" />
3_#{transitionItem.name} 4_#{transitionItem.name}
</c:forEach>
</c:forEach>
</ui:composition>
After page reload, transitionItem.Name returns the correct value for 3 and 4, and different values for 1 and 2. Maybe a JSF-JSTL integration problem?
页面重新加载后,transitionItem.Name 为 3 和 4 返回正确的值,为 1 和 2 返回不同的值。也许是 JSF-JSTL 集成问题?
采纳答案by noup
Found a workaround, by getting rid of the inner forEach loop, thus returning a linear list from the controller.
找到了一种解决方法,通过摆脱内部 forEach 循环,从而从控制器返回一个线性列表。
回答by Romain Linsolas
I see that you are using Facelets.
我看到您正在使用 Facelets。
Maybe you can try to replace your <c:forEach>by <ui:repeat>...
也许你可以尝试用你<c:forEach>的<ui:repeat>...
The code will then become:
代码将变为:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
(...)
xmlns:c="http://java.sun.com/jstl/core"
xmlns:h="http://java.sun.com/jsf/html">
(...)
<ui:repeat value="#{controller.availableTransitions}" var="transition">
<ui:repeat value="#{transition.availableTransitions}" var="transitionItem">
<h:outputText value="1_#{transitionItem.name} 2_#{transitionItem.name}" />
3_#{transitionItem.name} 4_#{transitionItem.name}
</ui:repeat>
</ui:repeat>
</ui:composition>
回答by digitaljoel
In general, I try to use ui:repeat most of the time. When I was having c:set issues, I found this blog, which was very helpful and may apply in your case also.
一般来说,我尝试使用 ui:repeat 大部分时间。当我遇到 c:set 问题时,我发现了这个博客,它非常有帮助,也可能适用于您的情况。
http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets
http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets

