2 Replies Latest reply on Jan 20, 2010 12:41 PM by Phil Haigh

    Actions not being called 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);
           }
      }