指定? 内元素的条件渲染似乎不起作用
我正在尝试使用
有条件地构建自定义列表。在列表中每次出现 -1 作为项目值时,我需要添加换行符。
我尝试在
内使用
来实现此目的,但它似乎不起作用。它始终评估false
。
<ul>
<ui:repeat value="#{topics.list}" var="topicId" >
<li>#{topicId}</li>
<c:if test="#{topicId eq -1}"> <br/> </c:if>
</ui:repeat>
</ul>
这可能吗?
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.
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>
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有 JSTL 标签,不行。它们在视图构建期间运行,而不是在视图渲染期间运行。您可以将其形象化如下:当 JSF 构建视图时,JSTL 标记首先从上到下运行,结果是一个纯 JSF 组件树。然后,当 JSF 渲染视图时,JSF 组件从上到下运行,结果是一堆 HTML。因此,JSTL 和 JSF 并不像您期望的编码那样同步运行。当您的JSF 组件在范围内不可用。
JSTL 标签运行时,#{topicId}
变量由您需要在感兴趣的 JSF 组件的
rendered
属性中指定条件,而不是使用
。由于实际上没有,您可以将其包装在
。替代方案是
或根据您的具体情况
因为当未指定客户端属性时,两者也不向 HTML 输出发出任何其他内容。
另请参阅:
与具体问题无关,这是
的错误位置。任何遵守 HTML 规范的网络浏览器都会忽略它。您不是说它在
内部吗?或者更好的是,给它一个
class
并让 CSS 给它一个margin-bottom
。Not with JSTL tags, no. They run during view build time, not during view render time. You can visualize it as follows: when JSF builds the view, JSTL tags run from top to bottom first and the result is a pure JSF component tree. Then when JSF renders the view, JSF components run from top to bottom and the result is a bunch of HTML. So, JSTL and JSF don't run in sync as you'd expect from the coding. At the moment your
<c:if>
JSTL tag tag runs, the#{topicId}
variable which is set by<ui:repeat>
JSF component isn't available in the scope.Instead of using
<c:if>
, you need to specify the condition in therendered
attribute of the JSF component of interest. As you've actually none, you could wrap it in a<ui:fragment>
.Alternatives are
<h:panelGroup>
or in your specific case
<h:outputText escape="false">
as both also emits nothing else to the HTML output when no client side attributes are specified.
See also:
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 aclass
and let CSS give it amargin-bottom
.