9 Replies Latest reply on Sep 20, 2012 7:12 AM by healeyb

    Problem with a4j:ajax and multiple forms

    goku88

      Hi,

      I have this page:

       

                <h:head></h:head>

              <h:body>

                  <h:form id="someForm">

                      Form is here

                  </h:form>

                  <h:form>

                      <a4j:commandButton id="editButton" value="editSomeString"

                                      execute="@this"

                                      action="#{testBean.edit('afterEdit')}"

                                      oncomplete="#{rich:component('editPanel')}.show()"

                                      render="editPanel"/>

                      <rich:popupPanel modal="true" id="editPanel" autosized="true"

                                    domElementAttachment="form">

                          <f:facet name="controls">

                              <a4j:ajax event="show" oncomplete="alert('#{testBean.someString}')" />

                          </f:facet>

                          Changed value is          

                          #{testBean.someString}

                      </rich:popupPanel>

                  </h:form>

              </h:body>

       

      With this bean:

       

      @ViewScoped

      @ManagedBean(name = "testBean")

      public class TestBean implements Serializable {

       

          public TestBean() {

              someString = "initValue";

          }

          private String someString;

       

       

          public String getSomeString() {

              return someString;

          }

       

       

          public void setSomeString(String someString) {

              this.someString = someString;

          }

       

       

          public void edit(String value) {

             someString = value;

          }

      }

        • 1. Re: Problem with a4j:ajax and multiple forms
          goku88

          Didn't finish questin yet. Sorry for that

           

          Now if I click the button in popup I will have corect value (afterEdit) and in alert box the old one (initValue).

          If I remove first form (id=someForm) or change bean scope to session it works just fine (afterEdit value showed in alert box).

          Any ideas what is going on?

           

          I use this dependencies for jsf:

           

                    <dependency>

                      <groupId>com.sun.faces</groupId>

                      <artifactId>jsf-api</artifactId>

                      <version>2.1.1-b04</version>

                      <scope>compile</scope>

                  </dependency>

                  <dependency>

                      <groupId>com.sun.faces</groupId>

                      <artifactId>jsf-impl</artifactId>

                      <version>2.1.1-b04</version>

                      <scope>compile</scope>

                  </dependency>

           

          along with  richfaces 4.0.0.Final and GlassFish Server Open Source Edition 3.1.2 (build 23)

           

           


          • 2. Re: Problem with a4j:ajax and multiple forms
            goku88

            Maybe I didn't write what exactly I want to achive here. I do not want solution because I know it:  just use one form with regions to restrict  processing data will fix the problem

            What I want to know is why does it happen?

            I  did research and as fas as I know this is problem with view state (there is one per form as hidden field) but since those to forms do not interact with each other I don't understand what is going on. I did leave the first form empty to make a point that it breaks the page just by sitting there and doing nothing.

            Thanks for any advice

            • 3. Re: Problem with a4j:ajax and multiple forms
              healeyb

              Have a look at this:

               

              http://java.net/jira/browse/JAVASERVERFACES-2199

               

              This is being dealt with as a spec issue so won't be fixed until JSF 2.2. I think it will explain the behavior you are seeing

              so have a read through.

               

               

               

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

              1 of 1 people found this helpful
              • 4. Re: Problem with a4j:ajax and multiple forms
                goku88

                Thanks for the answer.

                I saw it earlier and I know it is connected but in the bug there is one form witch rerender another also both form have to be submitted to reproduce this behaviour. In my page the first form does nothing and second one updates only fields within itself. Also only second form is submitted. So as I understand that view state in both forms are unsynchronized. The question is why oncomplete method of the a4j:ajax tag reads bad one.

                Also if You change pupuppanel to look like this:

                 

                 

                   <rich:popupPanel modal="true" id="editPanel" autosized="true"

                                             styleClass="taskEditPanel" domElementAttachment="form" onhide="alert('from on hide #{testBean.someString}')" onshow="alert('from on show #{testBean.someString}')">

                                <f:facet name="controls">

                                    <a4j:ajax event="show" oncomplete="alert('show complete #{testBean.someString}')"  />

                                    <a4j:ajax event="hide" oncomplete="alert('hide complete  #{testBean.someString}')"/>

                                </f:facet>

                                Changed value is         

                                #{testBean.someString}

                                 <a4j:commandButton id="exitButton" value="exit"

                                            execute="@this"

                                            oncomplete="#{rich:component('editPanel')}.hide()"

                                            />

                            </rich:popupPanel>

                 

                 

                And  You try to open and close popup those alerts appear:

                 

                 

                "from on show afterEdit"

                "show complete initValue"

                "from on hide afterEdit"

                "hide complete initValue"

                 

                 

                onhide and onshow have correct values and oncomplete methods from a4j:ajax bad ones.

                 

                 

                I know it is specific example but also simple to reproduce. Just trying to understand it.

                 

                Thanks for Your time,

                Paul

                • 5. Re: Problem with a4j:ajax and multiple forms
                  healeyb

                  I've just copied your code from the 1st post into a test page and I get "afterEdit" in the popupPanel every time!

                  I think what is happening in your case is that the extra form is confusing the a4j:commandButton so that it can't

                  find editPanel.  If you add an id to the 2nd form id="form2" and use render="form2:editPanel" does it work then?

                   

                  The a4j ajax components are a bit smarter than f:ajax at finding components in a multiform situation. What

                  happens if you replace the a4j:commandButton with an h:commandButton and a nested f:ajax, do you get a

                  'can't find editPanel' error in the server log? (f:ajax is a bit netter than the a4j components at telling you when a

                  render target can't be found...)

                   

                  I'm running JSF 2.1.12 & RF 4.2.1.Final, I'd recommend an upgrade, there must have been a bug fix along the

                  way.

                   

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

                  • 6. Re: Problem with a4j:ajax and multiple forms
                    goku88

                    Ok. In the popupPanel I also get "afterEdit" every time (by in the popup I mean in here:  "Changed value is #{testBean.someString} "). In alert which is opened from here: <a4j:ajax event="show" oncomplete="alert('#{testBean.someString}')" /> I get "initValue". I assume that You mean that in alert box You have "afterEdit" every time or did You look at the value in rich:popupPanel?

                    Anyway after adding id for second form along with render nothing changed. Also changing to h:commandButton and f:ajax didn't produce any error but I get correct "afterEdit" value from onevent (since there is no oncomplete function) on f:ajax  . Switching to JSF 2.1.12 and richfaces 4.2.1.Final didn't help either.

                    Could You please let me know what value did You get in the alert box?

                     

                    Thanks,

                    Paul

                    • 7. Re: Problem with a4j:ajax and multiple forms
                      healeyb

                      When I load the page I see some text "Form is Here" followed by a button "editSomeString", which I click. An alert

                      box shows containing the text "afterEdit". With the alert box showing I move it and behind this I see the text

                      "Changed value is afterEdit" (the popupPanel).

                       

                      So strangely we have a difference in behavior. Well, the alert box is just debug code, right? or is there a real

                      requirement to do something like this? I was surprised that you can use a4j:ajax as a child of popupPanel,

                      especially in a facet to be honest, but it must implement ClientBehaviorHolder.

                       

                      Why the difference in behavior, hmmm which browser are you using? I'm using chrome, but in firefox the alert

                      doesn't show at all, and in IE8 I'm getting javascript errors in the richfaces libs which is preventing the alert from

                      showing.

                       

                      When I remove the controls facet and the a4j:ajax and add onshow="alert('#{testBean.someString}')" to the

                      popupPanel then it works in all 3 browsers just fine, with "afterEdit" showing in the alert box and the popupPanel.

                       

                      Regards,

                      Brendan.

                      • 8. Re: Problem with a4j:ajax and multiple forms
                        goku88

                        Hmm,

                        Checked in firefox, IE9 and chrome and had same results.

                        The code in a4j:ajax oncomplete method have to be invoked after popup shows (so usning onevent, onshow isn't an option). But we already know solution. Right now I am just trying to figure it out Maybe reading about how a4j:ajax works will explain something. If I learn something new I will post it here.

                         

                        Thanks for help.

                        • 9. Re: Problem with a4j:ajax and multiple forms
                          healeyb

                          You could post your web.xml, but I'm pretty much at a loss to explain it. Did you clean and build after JSF/Richfaces upgrade?

                          With glassfish, did you copy the new JSF jar to the glassfish/modules directory?