7 Replies Latest reply on Sep 20, 2012 8:59 AM by spinacz

    a4j:commandButton doesn't execute action

    spinacz

      Hi,

       

      I am new with RichFaces and JSF, i have to update librares in a project.

      I updated JSF from 1.2 to 2.1.12 and RichFaces form 3.3.3 to 4.2.2, but after that, ajax stop working.

      I read migration guide, and i chnged tags and attributes as described, but ajax stil doesn't work.

       

      I have one large file where, is a few a4j:commandButton, this buttons navigate through form pages, some of them execute action at bean, but other not. I spent many hours trying to solve it, but it fell.

       

      my buttons are as follows:

      <a4j:commandButton value=" " action="#{mybean.page0}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" execute="@this" />
      
      <a4j:commandButton id="button-prev" styleClass="button-prev prev" value="&lt;&lt; Wstecz" image="/layout/img/mr/scoring/scoring-sg-button-prev.jpg" action="#{mybean.page0}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" execute="@this" />
      

      the first works, second doesn't, both are in the same file,a4j:region, a4j:outputPanel

      pages is id for <a4j:outputPanel>

      button is rendered if <c:if> condition is true, on page button is corectly rendered, but after click, doesn't exectute page0 action

       

      mybean is configured in faces-config:

       

      @ViewScoped
      public class Mybean implements Serializable {
                private static final long serialVersionUID = 1L;
      
                private int page;
      
                public int getPage() {
      //                    System.err.println("getPage działa, to co z AJAX?");
                          return page;
                }
      
      
                public void setPage(int page) {
      //                    System.err.println("setPage działa, to co z AJAX?");
                          if (page<=0)
                                    page = 1;
                          this.page = page;
                }
      
      
                public String page0() {
                          page = 1;
      //                    return "ok";
                          System.err.println("page0 działa, to co z AJAX?");
                          return null;
                }
      
      
                public String page1() {
                          page = 2;
      //                    return "ok";
                          System.err.println("page1 działa, to co z AJAX?");
                          return null;
                }
      
      
                public String page2() {
                          page = 3;
      //                    return "ok";
                          System.err.println("page2 działa, to co z AJAX?");
                          return null;
                }
      
      
                public String page3() {
                          page = 4;
      //                    return "ok";
                          System.err.println("page3 działa, to co z AJAX?");
                          return null;
                }
      
      
                public String page4() {
                          page = 5;
      //                    return "ok";
                          return null;
                }
      }
      

      after click on button it do something cause i see it in server log, but i don't see a println with corect page.

       

      any clues what i miss, or what should i look for?

       

      Thanks, Krzysiek

       

      sory for my english;)

        • 1. Re: a4j:commandButton doesn't execute action
          iabughosh

          welcome to the community Krzysiek,

           

          ensure that your command button is inside a h:form tag, and add <rich:messages globalOnly="true"/> to your page to ensure that there is no validation errors.

           

          regards.

          • 2. Re: a4j:commandButton doesn't execute action
            spinacz

            I should also write that all a4j:commandButton are inside h:form tag, and i have rich:messages and h:messages tags to check validation errors, there is no validation errors, buttons do not work.

            • 3. Re: a4j:commandButton doesn't execute action
              healeyb

              The button needs to be present in the DOM during the apply request values phase, and probably isn't because of the c:if. The issue

              is with the JSF code rather than the backing bean code, but check out this resource here:

               

              http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#2120183

               

               

               

              Regards,

              Brendan.

               

               

               


              Freelance Java Enterprise Developer


              JSF Richfaces Ajax Java 6/7 (scjp) EE 6 HTML CSS JavaScript jQuery MySQL JPA Hibernate Eclipselink

              Spring Oracle SQL JPQL Sybase EJB CDI Glassfish Apache JAX-RS Primefaces UNIX Paypal and more..

              • 4. Re: a4j:commandButton doesn't execute action
                spinacz

                I checked with resource, and whole code looks ok.

                Why the button may not be present in the DOM, when I use c:if? Buttons are present in rendered output.

                 

                the code with buttons:

                <div class="buttons-container ver2 #{mybean.page==1 ? 'page-1' : '' }">
                <c:if test="#{mybean.page==1}">
                  <a4j:commandButton styleClass="button-sg" value="Value" image="/layout/img/mr/scoring/button.png" action="#{mybean.page1}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" />
                </c:if>
                <c:if test="#{mybean.page==2}">
                  <a4j:commandButton id="button-prev" styleClass="button-prev prev" value="&lt;&lt; Wstecz" image="/layout/img/mr/scoring/button.jpg" action="#{mybean.page0}" render="pages" oncomplete="SomeAction(); AnotherAxtion();"  execute="@this" />
                  <a4j:commandButton id="button-next" styleClass="button-next next" value="Dalej >>" image="/layout/img/mr/scoring/button.png" action="#{mybean.page2}" render="pages" oncomplete="SomeAction(); AnotherAxtion();""/>
                </c:if>
                <c:if test="#{mybean.page==3}">
                  <a4j:commandButton id="button-prev" styleClass="button-prev prev" value="&lt;&lt; Wstecz" image="/layout/img/mr/scoring/button.jpg" action="#{mybean.page1}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" execute="@this" />
                  <a4j:commandButton id="button-next" styleClass="button-next next" value="Dalej >>" image="/layout/img/mr/scoring/button.png" action="#{mybean.page3}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" />
                </c:if>
                <c:if test="#{mybean.page==4}">
                  <a4j:commandButton id="button-prev" styleClass="button-prev prev" value="&lt;&lt; Wstecz" image="/layout/img/mr/scoring/button.jpg" action="#{mybean.page2}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" execute="@this" />
                  <a4j:commandButton id="button-next" styleClass="button-next next" value="Dalej >>" image="/layout/img/mr/scoringbutton.png" action="#{mybean.page4}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" />
                </c:if>
                <c:if test="#{mybean.page==5}">
                  <a4j:commandButton id="button-prev" styleClass="button-prev prev" value="&lt;&lt; Wstecz" image="/layout/img/mr/scoring/button.jpg" action="#{mybean.page3}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" execute="@this" />
                  <a4j:commandButton id="button-next" styleClass="button-next next" value="Dalej >>" image="/layout/img/mr/scoring/button.png" action="#{mybean.page5}" render="pages" oncomplete="SomeAction(); AnotherAxtion();" />
                </c:if>
                
                <div class="bth-clearfix"><f:verbatim>&amp;nbsp;</f:verbatim></div>
                </div>
                
                

                The first button for <c:if test="#{mabean.page==1}"> works ok, rest of buttons don't work.

                I tried instead of c:if use rendered atribute, i didn't see in server log information about page0 or page2 method was invoked, but i saw efect on the web page for first button in second c:if. for second button i have exception with id conflict althoguh rendered condition exclude the buttons each other.

                 

                regards.

                • 5. Re: a4j:commandButton doesn't execute action
                  spinacz

                  I found a solution, i changed the faces config managed-bean-scope from request to session, i don't know why, but it works.

                  Can someone explain to me what has changed in the RF4 and JSF2 that it was necessary to change for ajax?

                   

                  regards

                  KN

                  • 6. Re: a4j:commandButton doesn't execute action
                    healeyb

                    My previous answer wasn't much good so I'll try a bit harder this time! When you click an a4j:command* component it will send an ajax POST request,

                    and with a RequestScoped bean you would lose the value of mybean.page, because the data in the bean is specific to the initial request that loaded the

                    page, not the request that resulted from the clicking of the button. So this is why using a SessionScoped bean has solved the problem.

                     

                    The reason ViewScoped doesn't work is because you are using JSTL tags, see this bug/feature: http://java.net/jira/browse/JAVASERVERFACES-1665.

                    I really don't know why it used to work, maybe there was some special code in RF3 to handle this situation, I can see it would have been helpful, and

                    maybe they figured that ViewScoped would be the solution in RF4.

                     

                    Regards,

                    Brendan.

                     

                     

                     


                    Freelance Java Enterprise Developer


                    JSF Richfaces Ajax Java 6/7 (scjp) EE 6 HTML CSS JavaScript jQuery MySQL JPA Hibernate Eclipselink

                    Spring Oracle SQL JPQL Sybase EJB CDI Glassfish Apache JAX-RS Primefaces UNIX Paypal and more..

                    • 7. Re: a4j:commandButton doesn't execute action
                      spinacz

                      Thank you for explanation.

                       

                      After I did the update libraries, I did in many places code changes with migration guide with out tests, probably it was working because of a4j:keepAlive was used in many places, after changes stop working because of bug.