问题描述
我正在尝试使用<ui:repeat>有条件地构建自定义列表.在列表中每次出现-1作为item-value时,我都需要添加一个换行符.
I am trying to conditionally build a custom list using <ui:repeat>. On every occurrence of -1 as item-value in list, I need to add a line break.
我试图在<ui:repeat>中使用<c:if>,但是它似乎不起作用.它总是计算false.
I tried to use <c:if> inside <ui:repeat> for that, but it does not seem to work. It always evaluates false.
<ul> <ui:repeat value="#{topics.list}" var="topicId" > <li>#{topicId}</li> <c:if test="#{topicId eq -1}"> <br/> </c:if> </ui:repeat> </ul>
这可能吗?
推荐答案
不带JSTL标签,不可以.它们在视图构建期间运行,而不是在视图渲染期间运行.您可以将其可视化如下,当JSF构建视图时,JSTL标记从下到上首先运行,结果是一个纯JSF组件树.然后,当JSF渲染视图时,JSF组件从上到下运行,结果是一堆HTML.因此,JSTL和JSF不会像您期望的那样同步运行. <c:if>运行时,#{topicId}在范围中不可用.
Not with JSTL tags, no. They runs during view build time, not during view render time. You can visualize it as follows, when JSF builds the view, JSTL tags runs from to bottom first and the result is a pure JSF component tree. Then when JSF renders the view, JSF components runs from top to bottom and the result is a bunch of HTML. So, JSTL and JSF doesn't run in sync as you'd expect from the coding. At the moment your <c:if> runs, the #{topicId} isn't available in the scope.
您需要在感兴趣的JSF组件的rendered属性中指定条件,而不是使用<c:if>.由于实际上没有,您可以将其包装在 <ui:fragment> .
Instead of using <c:if>, you need to specify the condition in the rendered attribute of the JSF component of interest. As you've actually none, you could wrap it in a <ui:fragment>.
<ul> <ui:repeat value="#{topics.list}" var="topicId" > <li>#{topicId}</li> <ui:fragment rendered="#{topicId eq -1}"><br/></ui:fragment> </ui:repeat> </ul>
替代方法是 <h:panelGroup>
Alternatives are <h:panelGroup>
<h:panelGroup rendered="#{topicId eq -1}"><br/></h:panelGroup>
或在您的特定情况下 <h:outputText escape="false">
or in your specific case <h:outputText escape="false">
<h:outputText value="<br/>" escape="false" rendered="#{topicId eq -1}" />
因为当未指定客户端属性时,两者都不会向HTML输出发出任何其他信息.
as both also emits nothing else to the HTML output when no client side attributes are specified.
不相关与具体问题有关,这是错误的 位置.任何遵循HTML规范的Web浏览器都将忽略它.您不是说要在里面 吗?或者更好,给它一个class,然后让CSS给它一个margin-bottom.
Unrelated to the concrete problem, that's the wrong place for a <br/>. It would be ignored by any webbrowser respecting the HTML specification. Don't you mean it to be inside the <li>? Or better, give it a class and let CSS give it a margin-bottom.
这篇关于在< ui:repeat>中指定元素的条件渲染? < c:if>似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!