Is it possible to use <c:if> inside <a4j:repeat>

雨燕双飞 提交于 2019-12-12 20:59:34

问题


Is it possible to use <c:if> inside <a4j:repeat>?

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>

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

How can I achieve the requirement anyway?


回答1:


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.


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.

There are basically 2 solutions:

  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>
    

    (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)

  2. 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>
    

See also:

  • JSTL in JSF2 Facelets... makes sense?


来源:https://stackoverflow.com/questions/13751889/is-it-possible-to-use-cif-inside-a4jrepeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!