3 Replies Latest reply on Apr 3, 2010 5:46 PM by Florian Petersen

    Actions not being fired when button is conditionally rendered

    Phil Haigh Novice

      Hi,


      I have a form that has a button rendered via ajax if an object is selected from a drop down. The button gets rendered fine, but when clicked, does not fire the associated action. It does however refresh the page.


      I've simplified this to a basic test case, code below. When you click the button id button1, the page is refreshed (the timestamp output text updates), but nothing is logged. When you click button2, the page is refreshed and you see Doing some stuff in the logs.


      Can someone explain what I am missing here, and why this button would not fire the action?


      FORM:


      <h:form id="myform">     
           <h:outputText value="Timestamp: #{sandpitController.field1}"/>
           <br/>
           
           <s:decorate id="modelDecoration" template="/layout/edit.xhtml">
                <ui:define name="label">Select model</ui:define>
                <a4j:region>
                     <h:selectOneMenu styleClass="ajaxSupport" id="survey" label="Survey" value="#{model}" required="false">
                          <s:selectItems var="modl" label="#{modl.id}" value="#{models.resultList}" noSelectionLabel="Please select" />
                          <s:convertEntity/>
                          <a4j:support event="onchange" reRender="myform" ajaxSingle="true"/>
                     </h:selectOneMenu>
                </a4j:region>
           </s:decorate>                    
           <h:panelGroup rendered="#{not empty model.id}">
                This is a test and renders only if model.id is not empty. It is: '#{model.id}'
                <h:commandButton id="button1" action="#{sandpitController.doNothingButLog()}" value="Submit" immediate="true" />
           </h:panelGroup>
                     
           <h:commandButton id="button2" action="#{sandpitController.doNothingButLog()}" value="Submit"  />               
      </h:form>



      CONTROLLER:


      @Stateful
      @Name("sandpitController")
      public class SandpitController implements SandpitControllerI 
      {
           @Logger Log log;
           
           
           public String field1;
      
               @Destroy @Remove
               public void destroy() {}
          
          
               public void doNothingButLog()
               {
                log.info("Doing some stuff");        
               }
          
         
           
           public String getField1() 
           {
                field1 = System.currentTimeMillis()+"";
                return field1;
           }
      
           public void setField1(String field1) 
           {
                this.field1 = field1;
           }
      }
      



      MODEL:


      @Entity
      @Table(name="models")
      @Name("model")
      public class Model implements Serializable
      {
           private static final long serialVersionUID = 1L;
      
           @Id @GeneratedValue
           private Long id;     
           private Double number;
           
           public Model()
           {
           }
           
           public Double getNumber()
           {
                return number;
           }
      
           public void setNumber(Double number)
           {
                this.number = number;
           }
      
           public Long getId()
           {
                return id;
           }
      
           public void setId(Long id)
           {
                this.id = id;
           }
      }



      QUERY:


      @Name ("models")
      @Scope (ScopeType.CONVERSATION)
      public class Models extends EntityQuery<Model>
      {
           private static final long serialVersionUID = -5756514939628235812L;
           private static final String EJBQL = "select c from Model c";     
           private static final String ORDER = "id desc";
      
           public Models() 
           {
                setEjbql(EJBQL);
                setOrder(ORDER);
           }
      }



        • 1. Re: Actions not being fired when button is conditionally rendered
          Tim Evers Master

          Have you tried without the immediate=true on your button that doesn't work. (Don't get me wrong it should work with it there I think but just try it and see).


          If not try removing the a4j:region. (I gota do more reading on this, but does it really make any difference having the region there when you are using ajaxSingle=true on the component?)



          Basically, reduce your page down the the minimum you can have where it works. Then add stuff back in till it breaks so you know where to start looking.

          • 2. Re: Actions not being fired when button is conditionally rendered
            Phil Haigh Novice

            Hi Tim,


            Thanks for the reply. I have tried these things to no avail unfortunately.


            I think the problem is with Seam scoping on named components that are entity beans. If I use, for instance #{myController.model} instead of #{model}, then it works. However, the entity bean component is conversation scoped, just as the controller is - so to my mind, it shouldn't make a difference.?

            • 3. Re: Actions not being fired when button is conditionally rendered
              Florian Petersen Newbie
              Hi,
              i had the same problem with a Framework Home class. These links

              <h:commandLink id="persistUser" action="#{ppUserHome.persist}" rendered="#{!ppUserHome.idDefined}"><img src="../../gfx/btn-speichern.gif" /></h:commandLink>
              <h:commandLink id="updateUser" action="#{ppUserHome.update}" rendered="#{ppUserHome.idDefined}"><img src="../../gfx/btn-speichern.gif" /></h:commandLink>

              do not trigger the page actions, while this

              <h:commandLink id="saveUser" action="#{ppUserHome.persistOrUpdate}"><img src="../../gfx/btn-speichern.gif" /></h:commandLink>

              triggers them correctly.
              Moving the rendering condition into the action method is in fact a solution, but it took me about three hours to find out whats wrong, and Seam made my hair a bit more grey another time...

              So should this maybe considered a bug, or does anybody have a feasible explanation for this?
              Thanks a lot, best regards
              Flo