5 Replies Latest reply on May 10, 2011 5:05 PM by stasikos

    a4j:commandButton rerender issues

    stasikos

      Here is a sample code I used to understand how to use a4j:commandButton RF4:

       

       

      <?xml version='1.0' encoding='UTF-8' ?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:rich="http://richfaces.org/rich"
            xmlns:a4j="http://richfaces.org/a4j">
          <h:head>
              <title>Facelet Title</title>
          </h:head>
          <h:body>
              <h:form id="topForm">
                  <a4j:commandButton value="Show form" oncomplete="#{rich:component('table_mp')}.show()"/>
                  <rich:popupPanel id="table_mp" modal="true"
                                   width="370" minWidth="370"
                                   resizeable="false" autosized="false" moveable="true" >
                      <h:form id="bugform" >
                          <rich:dataTable id="attributes" value="#{TableController.attributes}" var="attr">
                              <rich:column> 
                                  <h:inputText value="#{attr}" />
                              </rich:column>
                          </rich:dataTable>
                          <a4j:commandButton value="Add attribute" 
                                             action="#{TableController.addAttribute()}"
                                             execute="@this"
                                             render="attributes, bugform, topForm"/>
                      </h:form>
                  </rich:popupPanel>
              </h:form>
          </h:body>
      </html>
      

       

      And a controller for it:

       

      package name.stasikos.bug;
      
      import java.util.ArrayList;
      import java.util.List;
      import javax.faces.bean.ManagedBean;
      
      /**
       *
       * @author stasikos
       */
      @ManagedBean(name="TableController")
      public class TableController {
      
      
          private List<String> attributes = new ArrayList<String>();
      
          public List<String> getAttributes() {
              return attributes;
          }
      
          public void setAttributes(List<String> attributes) {
              this.attributes = attributes;
          }
      
          public TableController() {
              this.attributes.add("Initial attr");
          }
      
          public TableController(List<String> attributes) {
              this.attributes = attributes;
          }
      
          public String addAttribute() {
              this.attributes.add(("attributeName" + attributes.size()).toString());
              System.out.println("attribute added" + System.currentTimeMillis());
              System.out.println(attributes.size());
              return "OK";
          }
      
      }
      

       

      It actually executes controller's addAttribute() method when I click "Add attribute" button and does it once. Server sends an response to request with new form contents (including added "attributeName1" item), however, new content is not displayed by browser.

       

      The expected result was to see the same modal panel with new inputText components adding, listing every item from attributes property. I.e. user clicks "Add attribute" button and adds new text field every time. Similar approach was working with RF3, but now I'm confused about it.

        • 1. Re: a4j:commandButton rerender issues
          iabughosh

          try using session or view scope with TableController .

          • 2. a4j:commandButton rerender issues
            stasikos

            I've added @SessionScoped to TableController, items are added as before. Now I can even reload page and see new items in the form. However, form is still not rerendered when I press "Add" button.

            • 3. a4j:commandButton rerender issues
              iabughosh

              try changing render value with full ids :

              render=":topForm:bugform:attributes, :topForm:bugform, :topForm"

              • 4. a4j:commandButton rerender issues
                ilya_shaikovsky

                nested forms are not allowed in JSF.

                • 5. Re: a4j:commandButton rerender issues
                  stasikos

                  Yes, thanks. Finally I changed code to this and it works as expected:

                  <?xml version='1.0' encoding='UTF-8' ?>
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <html xmlns="http://www.w3.org/1999/xhtml"
                        xmlns:h="http://java.sun.com/jsf/html"
                        xmlns:rich="http://richfaces.org/rich"
                        xmlns:a4j="http://richfaces.org/a4j">
                      <h:head>
                          <title>Facelet Title</title>
                      </h:head>
                      <h:body>
                          <h:form id="topForm">
                              <a4j:commandButton value="Show form" oncomplete="#{rich:component('table_mp')}.show()"/>
                           </h:form>
                              <rich:popupPanel id="table_mp" modal="true"
                                               width="370" minWidth="370"
                                               resizeable="false" autosized="false" moveable="true" >
                                  <h:form id="bugform" >
                                      <rich:dataTable id="attributes" value="#{TableController.attributes}" var="attr">
                                          <rich:column> 
                                              <h:inputText value="#{attr}" />
                                          </rich:column>
                                      </rich:dataTable>
                                  </h:form>                
                                  <h:form id="controlForm">
                                      <a4j:commandButton value="Add attribute" 
                                                         action="#{TableController.addAttribute()}"
                                                         render="bugform"/>
                                  </h:form>
                              </rich:popupPanel>
                  
                      </h:body>
                  </html>