是否可以使用< c:if>在< a4j:repeat>内部 [英] Is it possible to use <c:if> inside <a4j:repeat>

查看:73
本文介绍了是否可以使用< c:if>在< a4j:repeat>内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在<a4j:repeat>中使用<c:if>?

我需要每个4个元素渲染一个<br />.我尝试了以下方法:

I need to render a <br /> each 4 elements. I tried the following approach:

<a4j:repeat value="#{MBean.sucursal.events}" var="item" rowKeyVar="idx" >   
   <h:outputText value="#{item.eventDescription}" id="item1" />
   <c: if test=#{idx % 4 == 0}>
       <br />
   </c:if>
</a4j:repeat>

但是,它根本不呈现<br />元素.

However, it does not render the <br /> elements at all.

我怎么才能达到要求?

推荐答案

注意:我将忽略粗心的代码表述,这将导致它根本无法正确编译/运行. <c :if>中有一个空格,并且<c:if test>属性中没有引号.在以后的问题中,请多加注意发布的代码段的正确性,否则由于那些红色的鲱鱼,您只会得到无效的答案.

Note: I'll ignore the careless formulation of the code which causes that it wouldn't compile/run properly at all. There's a space in <c :if> and the quotes are missing from the <c:if test> attribute. In future questions please pay a bit more attention to the properness of the code snippets you post, otherwise you'll only get invalid answers due to those red herrings.

回到您的具体问题,此构造确实会失败. <c:if>在视图构建时运行(当即将基于Facelets/JSP文件填充JSF组件树时),而<a4j:repeat>在视图呈现时运行(当HTML代码将基于以下生成时): JSF组件树).因此,在<c:if>运行时,#{idx}根本不在范围内,并且始终计算为null.

Coming back to your concrete problem, this construct will indeed fail. The <c:if> runs during view build time (when the JSF component tree is about to be populated based on Facelets/JSP files), while the <a4j:repeat> runs during view render time (when the HTML code is about to be generated based on the JSF component tree). So, at the moment the <c:if> runs, #{idx} does not exist in the scope at all and always evaluates to null.

基本上有2种解决方案:

There are basically 2 solutions:

  1. 使用<c:forEach>而不是<a4j:repeat>来遍历项目.它在视图构建期间运行,因此它将与<c:if>同步"运行.

  1. Use <c:forEach> instead of <a4j:repeat> to iterate over the items. It runs during view build time, so it'll run "in sync" with <c:if>.

<c:forEach items="#{MBean.sucursal.events}" var="item" varStatus="loop">   
   <h:outputText value="#{item.eventDescription}" id="item#{loop.index}" />
   <c:if test="#{loop.index % 4 == 0}">
       <br />
   </c:if>
</c:forEach>

(请注意,我也更改了<h:outputText id>,否则最终会出现重复的组件ID错误或HTML格式错误;请完全忽略id属性,JSF会自动生成正确的属性)

(note that I changed the <h:outputText id> as well, otherwise you end up with duplicate component ID errors or malformed HTML; feel free to omit the id attribute altogether, JSF will autogenerate proper one)

使用JSF组件的rendered属性而不是<c:if>有条件地呈现HTML.它在视图渲染期间运行,因此它将与<a4j:repeat>同步"运行.

Use JSF component's rendered attribute instead of <c:if> to conditionally render HTML. It runs during view render time, so it'll run "in sync" with <a4j:repeat>.

<a4j:repeat value="#{MBean.sucursal.events}" var="item" rowKeyVar="idx">   
   <h:outputText value="#{item.eventDescription}" id="item1" />
   <h:panelGroup rendered="#{idx % 4 == 0}">
       <br />
   </h:panelGroup>
</a4j:repeat>

另请参见:

  • JSF2 Facelets中的JSTL ...有意义吗?
  • See also:

    • JSTL in JSF2 Facelets... makes sense?
    • 这篇关于是否可以使用&lt; c:if&gt;在&lt; a4j:repeat&gt;内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆