5 Replies Latest reply on May 2, 2011 10:21 AM by djnose

    a4j:commandButton reads property twice / call getter twice while trying to render a table

    djnose Newbie

      I hope my Problem is not that big maybe it is just a little problem which could be easy solved.

      The point is, that if i use a a4j:commandButton, my getProperty function is called twice.

      I have simple table which should be reloaded, if i filter it.

       

      Pls. look at the following code for a better understanding:

       

      <h:form id="filterProvider">

                          <h:panelGrid columns="6" >

       

                              <h:outputText value="Provider" />

                              <rich:select selectFirst="true" value="#{filterBean.defaultStringFilter}" id="filter_provider_providerName1">

                                  <f:selectItems value="#{filterBean.defaultStringFilterList}" />

                              </rich:select>

                              <h:inputText value="#{tableDemo.provider.providerName1}">

                              </h:inputText>

       

                              <h:outputText value="Provider Name" />

                              <rich:select selectFirst="true" value="#{filterBean.defaultStringFilter}" id="filter_provider_providerName">

                                  <f:selectItems value="#{filterBean.defaultStringFilterList}"/>

                              </rich:select>

                              <h:inputText value="#{tableDemo.provider.providerName}">

                              </h:inputText>                       

                              <h:outputText value="Provider ID" />

                              <rich:select selectFirst="true" value="#{filterBean.defaultIntFilter}" id="filter_provider_id">

                                  <f:selectItems value="#{filterBean.defaultIntFilterList}"/>

                              </rich:select>

                              <h:inputText value="#{tableDemo.provider.id}">

                              </h:inputText>               

       

                          </h:panelGrid>

                          <a4j:commandButton value="Suchen" render="demoTable"/>

                     </h:form>

                      <h:form id="formProviderTable">

                          <h:panelGrid>

                              <a4j:outputPanel id="demoTablePanel">

                                  <rich:dataScroller for="demoTable" maxPages="5"/>

                                  <rich:dataTable style="width: 750px;" value="#{tableDemo.providerList}" var="provider"  id="demoTable" rows="#{configBean.rowSize}" rowClasses="row1, row2"

                                                  onrowmouseover="this.style.backgroundColor='#B5F3FB'" 

                                                  onrowmouseout="this.style.backgroundColor='#{a4jSkin.rowBackgroundColor}'"  >

                                      <rich:column>

                                          <f:facet name="header">

                                              <h:outputText value="ID" />

                                          </f:facet>

                                          <h:outputText value="#{provider.id}" />

                                      </rich:column>

      // and so on...

       

      it just calls a simple ManagedBean where

      public List<Provider> getProviderList() {

      is called twice!

      What am i doing wrong? Thx for your help

        • 1. Re: a4j:commandButton reads property twice / call getter twice while trying to render a table
          djnose Newbie

          Ok so far i figured out, that

           

          <rich:datascroller for="demoTable" maxPages="5"/>

           

          makes my function called twice. If i comment that part out and render the form (not the table). It works as expected. Thats kind of weird.

          Any clues?

           

          Edit:

          Ok, now its getting real confusing.

          If i only replace the datascroller above the table, everything works fine. Even if the datascroller at the bottom of the table is still enabled.

          If i add the datascroller above the table again, my function is called twice...wth...

          Hopy anybody has a good idea for me... maybe i missed something out or similar...

          • 2. a4j:commandButton reads property twice / call getter twice while trying to render a table
            Lukáš Fryč Master

            Hello,

             

            it seems that dataScroller needs to check your model in order to configure pagination (this is cause why getter is invoked twice).

             

            Generally it is not convenient to have getter depending on number of its invocations.

             

            You will need to cache the content generated in your getter in your bean, so your code will not trigger your code twice.

            • 3. Re: a4j:commandButton reads property twice / call getter twice while trying to render a table
              djnose Newbie

              But what if i do not have a "stupid list" ?

              I want to execute a SQL Statement because somebody entered a filter or similar.

              I do not always want to fetch the whole data and let Javascript to apply a filter. Thats not convenient. If my getter executes a SQL - Statement and it is called twice...how am i able to avoid something like that?

               

              f.e.

                  public List<Provider> getProviderList() {

                       providerList = getDataList(selectProv);

                  }

               

              getDataList does some SQL - Stuff, depending on the given data. If that is called twice, it is not that good for me and my database .. imagine that 1000 people do a query and its queried always twice. Thats an extrem huge overhead which should be avoided.

              Or can Richfaces interact with Hibernate or EJB-Queries directly (like doing an order by, or build a like or something similar. That would be awesome )

              Thx for your reply!              

              • 4. Re: a4j:commandButton reads property twice / call getter twice while trying to render a table
                Lukáš Fryč Master

                If you have @RequestScoped backing bean, then you can simply do:

                 

                List<Provider> providerList;
                
                public List<Provider> getProviderList() {
                    if (providerList == null) {
                         providerList = getDataList(selectProv);
                   }
                    return providerList;
                    }
                

                 

                providerList will live for the length of request, so you will need fire the database request at most once for a request.

                 

                If you have more complex bean (living for life of view or session), you can delegate the provider list to new request scoped bean.

                 

                Actually it is good manner to have all backing beans request scoped.

                • 5. a4j:commandButton reads property twice / call getter twice while trying to render a table
                  djnose Newbie

                  I think i totally missunderstood the concept, cause i saw there is just one HttpRequest but many times the setter called.

                  I do not know why i changed my backing bean to a @ViewScoped bean, but changing it back to @RequestScope (it that correct english? sry if not )

                  worked like a charm.

                  I first thought if the getter is called twice, the requestresult changes .. but thats .. a forget it ..

                  it works well now!

                  Thank you