11 Replies Latest reply on Sep 24, 2008 9:07 AM by dro_k

    UI Repeat

    bashan

      Hi,


      I know this is more of a Facelets question...
      Is there a way of using ui repeat tag just as it was a simple indexed for loop?
      I mean, I don't want to create a bean and fill it with five items, in order to make a loop from 0 to 4...


      Thanks,
      Guy.

        • 1. Re: UI Repeat
          sjmenden

          Not with ui:repeat but yes with c:forEach, see https://facelets.dev.java.net/nonav/docs/dev/docbook.html#taglib-available-jstl  so you can do:


          <c:forEach begin="0" end="4" varStatus="idx">
            #{idx}
          </c:forEach>


          • 2. Re: UI Repeat
            bashan

            Weird, for some reason the c:forEach doesn't work for me. I added the namespace, run a simple loop, wrote some text in the loop and all I see is a single time the text it printed. I assume that facelets simply ignore this tag...


            But anyway, I am trying to use t:dataList (like ui:repeat) for now. I am generating a list on the server side. The list is being generated from a facelets component. I pass to the
            component a signle parameter:


            <n:ratingShow rating="#{video.rating}" />



            It seems like this parameter is not accessible on the server.
            This is my component:


            <span xmlns="http://www.w3.org/1999/xhtml"
                  xmlns:ui="http://java.sun.com/jsf/facelets"
                  xmlns:f="http://java.sun.com/jsf/core"
                  xmlns:h="http://java.sun.com/jsf/html"
                  xmlns:s="http://jboss.com/products/seam/taglib"
                  xmlns:t="http://myfaces.apache.org/tomahawk"
                  xmlns:c="http://java.sun.com/jsp/jstl/core">
              
              <t:dataList value="#{ratingList}" var="r" rowIndexVar="rowIndex">
                <h:graphicImage url="img/rating/small_#{r}.jpg" />
              </t:dataList>
              </span>


            This is the server side code:


            @Name("ratingList")
            public class RatingAction
            {
              @Unwrap
              public List<String> getRatingList()
              {
                double rating = (Double)FacesUtil.getElValue("#{rating}");
                List<String> list = new ArrayList<String>();
                for (int i = 0; i < 5; i++)
                {
                  if (rating >= i + 1)
                  {
                    list.add("full");
                  }
                  else if (rating >= i + 0.5)
                  {
                    list.add("half");
                  }
                  else
                  {
                    list.add("empty");
                  }
                }
            
                return list;
              }
            }



            The rating is null. although when printing it from the component xml code, it returns a valid value.


            The code I am using to evaluate the expression:


              public static Object evalEl(String expression) {
                  String framedExpr = "#{" + expression + "}";
                  Object value = Expressions.instance().createValueExpression(framedExpr).getValue();
                  return value;
                }








            • 3. Re: UI Repeat
              bashan

              Sorry, I forgot to ask the final question... ;-)
              Is there a reason the rating is not being evaluated in the Java code? Should there be some other code to evaluate facelets parameter (if it does it will be a bit not comfortable...).


              One more thing the interests me:
              When calling the ratingList twice, it actually executes the getRatingList method twice. Is there a way to cause Seam to cache the result for the request and execute the getRatingList only once (I though that this is one of the benfits of using @Unwrap)?


              Thanks,
              Guy.

              • 4. Re: UI Repeat
                ctomc

                Hello,


                you can also try a4j:repeat it has index and everything you need...


                the difference between c:forEach and ui:repeat is that c:forEach is content handler and ui:repeat is component..



                cheers,
                tomaz

                • 5. Re: UI Repeat
                  bashan

                  Thanks,
                  Is there any component allowing conditional flow rather than using rendered property?
                  Does anyone know how to evaluate facelet component parameter in java code?

                  • 6. Re: UI Repeat
                    tony.herstell1

                    <s:fragment> for conditional stuff in your xhtml pages.


                    • 7. Re: UI Repeat
                      tony.herstell1

                      Example



                      <s:fragment rendered="#{!(identity.loggedIn and s:hasRole('ADMINISTRATOR'))}">
                           <div class="centre text_important">
                                <h:outputText value="#{messages.status_arena_status}" />
                                <s:fragment rendered="#{!(identity.loggedIn and s:hasRole('ADMINISTRATOR'))}">
                                     <span class="italic small_text">
                                                    (<h:graphicImage alt="#{messages.status_open}" url="#{basePath}/images/tick.gif" height="10"/><h:outputText value="#{messages.status_open}" />
                                          <h:graphicImage alt="#{messages.status_closed}" url="#{basePath}/images/cross.gif" height="10"/><h:outputText value="#{messages.status_closed}" />)
                                     </span>
                                </s:fragment>
                           </div>
                      </s:fragment>
                      


                      • 8. Re: UI Repeat
                        bashan

                        Nice thing, though I don't see here any special properties that I cannot do using simple h:panelGroup.

                        • 9. Re: UI Repeat
                          bashan

                          Sorry, couldn't find any index property in a4j:repeat...

                          • 10. Re: UI Repeat
                            brachie
                            Hi,

                            what is the difference between <s:fragment> and <s:div> or <s:span> (except for the block and inline type)?

                            Alexander
                            • 11. Re: UI Repeat
                              dro_k

                              <s:div> conditionally renders its contents wrapped in HTML <div>


                              <s:span> conditionally renders its contents wrapped in HTML <span>


                              <s:fragment> conditionally renders its contents without wrapping them in any HTML element


                              cheers,


                              Drew