2 Replies Latest reply on Sep 9, 2012 2:43 AM by Jose Alvarez de Lara

    can't display the result of a <a4j:commandButton> action in RF 4.2.2

    Jose Alvarez de Lara Novice

      Hi,

       

      I have developed a richfaces xhtml file managed by its corresponding bean.

      In the first action I can display a selected field type of List of an entity.

       

      In the second action I need to select one of the items in the list. This list

      is displayed in a <rich:dataTable> as follows,

       

      <rich:dataTable var="cat" value="#{familiaAgent.categorias}">
         <rich:column>
            <f:facet name="header">Nombre</f:facet>
            <h:outputText value="#{cat.nombre}" />
         </rich:column>
         <rich:column>
            <f:facet name="header">Seleccionar</f:facet>
            <a4j:commandButton value="Seleccionar" render="ctg" action="#{familiaAgent.selectCategoria}">
               <a4j:param name="sId" value="1" assignTo="#{familiaAgent.sId}" noEscape="true" />
               <a4j:param name="cId" value="#{cat.id}" assignTo="#{familiaAgent.id}" noEscape="true" />
            </a4j:commandButton>                                                                            
         </rich:column>
      </rich:dataTable>
      

       

      The second action is the one executed by the second column.

       

      But it does not do what I spect, in fact it does nothing.

       

      Here is the output (render="ctg")

       

      <h:panelGrid id="editPane" rendered="#{familiaAgent.categoriaSelected eq true}">
         <rich:panel id="ctg">
            <h:outputText value="ID: #{familiaAgent.categoria.id}" />
            <h:outputText value="Nombre: #{familiaAgent.categoria.nombre}" />                                                                         
         </rich:panel>
      </h:panelGrid>
      

       

       

      And here is the bean,

       

      @RequestScoped
      @Named("familiaAgent")
      public class FamiliaAgent {
      
          @Inject
          private FamiliaDAO familiaDAO;
      
          private Familia familia;
      
          private List<Categoria> categorias;
      
          private Categoria categoria;
      
          private Integer id;
      
          private String sId;
      
          private boolean familiaSelected;
      
          private boolean categoriasSelected;
      
          private boolean categoriaSelected;
      
          public void selectCategorias() {
              // NOTE get a fresh reference that's managed by the extended persistence context
              try {
                  if(sId != null) {
                      Integer iId = Integer.valueOf(sId);
                      familia = familiaDAO.buscarPorId(iId);
                      if (familia != null) {
                          categorias = familia.getCategorias();
                          familiaSelected = true;
                          categoriasSelected = true;
                      }
                  }
              } catch(Exception ex) {
                  familiaSelected = false;
                  categoriasSelected = false;
              }
          }
      
          public void selectCategoria() {
              // NOTE get a fresh reference that's managed by the extended persistence context
              try {
                  if(sId != null) {
                      Integer iId = Integer.valueOf(sId);
                      familia = familiaDAO.buscarPorId(iId);
                      if (familia != null) {
                          categorias = familia.getCategorias();
                          if(id != null) {
                              categoria = categorias.get(id);
                          }
                          familiaSelected = true;
                          categoriaSelected = true;
                      }
                  }
              } catch(Exception ex) {
                  familiaSelected = false;
                  categoriaSelected = false;
              }
          }
      
          public void cancel() {
              categoria = null;
              categorias = null;
              familia = null;
              familiaSelected = false;
              categoriasSelected = false;
              categoriaSelected = false;
          }
      
          public Familia getFamilia() {
              return familia;
          }
      
          public void setFamilia(final Familia familia) {
              this.familia = familia;
          }
      
          public List<Categoria> getCategorias() {
              return categorias;
          }
      
          public void setCategorias(final List<Categoria> categorias) {
              this.categorias = categorias;
          }
      
          public Categoria getCategoria() {
              return categoria;
          }
      
          public void setCategoria(final Categoria categoria) {
              this.categoria = categoria;
          }
      
          public boolean isFamiliaSelected() {
              return familiaSelected;
          }
      
          public boolean isCategoriasSelected() {
              return categoriasSelected;
          }
      
          public boolean isCategoriaSelected() {
              return categoriaSelected;
          }
      
          public void setId(final Integer id) {
              this.id = id;
          }
      
          public Integer getId() {
              return id;
          }
      
          public void setsId(final String sId) {
              this.sId = sId;
          }
      
          public String getsId() {
              return sId;
          }
      }
      

       

      I am not sure if it is due the commandButton is in the table or what else

      but it does not work.

       

      Both the table and the displayed result are all enclosed in the same form.

       

      Thanks in advance,

       

      Regards,

      Jose

       

      El mensaje fue editado por: Jose Alvarez de Lara

        • 1. Re: can't display the result of a <a4j:commandButton> action in RF 4.2.2
          Brendan Healey Master

          Try number 4 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..

          • 2. Re: can't display the result of a <a4j:commandButton> action in RF 4.2.2
            Jose Alvarez de Lara Novice

            Thanks a lot Brendan.

             

            I had a wrong code in the bean and the xhtml file.

             

            On the other hand I am really appreciated for your help.

            Yes, the link explain clearly the usage of a commandButton and a commandLink.

             

            So I did copy the code in the showcase example and now it works with a little issue.

            When the table is displayed (now I use buttons in it) for the first time I need to click twice

            to get the response, The next times just one click is enough.

             

            I will debug the web app.

             

            Let me share my code,

             

            The bean.-

             

            @SessionScoped
            @Named("familiaAgent")
            public class FamiliaAgent implements Serializable {
            
                private static final long serialVersionUID = 630446893420302625L;
            
                @Inject
                private FamiliaDAO familiaDAO;
            
                private Familia familia;
                
                private List<Categoria> categorias;
                
                private Categoria categoria;
                
                private Integer catId;
                
                private String sFamId;
                
                private String sCatId;
                
                private String nombreFamilia;
                
                private String nombreCategoria;
                
                private boolean familiaSelected;
                
                private boolean categoriasSelected;
                
                private boolean categoriaSelected;
                    
                public void selectCategorias() {
                    try {
                        if(sFamId != null) {
                            Integer iId = Integer.valueOf(sFamId);
                            familia = familiaDAO.buscarPorId(iId);
                            if (familia != null) {
                                categorias = familia.getCategorias();
                                familiaSelected = true;
                                categoriasSelected = true;
                            }
                        }
                    } catch(Exception ex) {
                        familiaSelected = false;
                        categoriasSelected = false;
                    }
                }
                
                public void selectCategoria() {
                    try {
                        if(sFamId != null) {
                            Integer iId = Integer.valueOf(sFamId);
                            familia = familiaDAO.buscarPorId(iId);
                            if (familia != null) {
                                familiaSelected = true;
                                categorias = familia.getCategorias();
                                if(sCatId != null) {
                                    iId = Integer.valueOf(sCatId);
                                    categoria = categorias.get(iId);
                                    categoriaSelected = true;
                                    
                                    editCategoria();
                                }
                            }
                        }
                    } catch(Exception ex) {
                        familiaSelected = false;
                        categoriaSelected = false;
                    }
                }
                
                private void editCategoria() {
                    catId = categoria.getId();
                    nombreFamilia = categoria.getFamilia().getNombre();
                    nombreCategoria = categoria.getNombre();
                }
                
                public void cancel() {
                    categoria = null;
                    categorias = null;
                    familia = null;
                    familiaSelected = false;
                    categoriasSelected = false;
                    categoriaSelected = false;
                }
            
                public Familia getFamilia() {
                    return familia;
                }
                
                public void setFamilia(final Familia familia) {
                    this.familia = familia;
                }
            
                public List<Categoria> getCategorias() {
                    return categorias;
                }
                
                public void setCategorias(final List<Categoria> categorias) {
                    this.categorias = categorias;
                }
                
                public Categoria getCategoria() {
                    return categoria;
                }
                
                public void setCategoria(final Categoria categoria) {
                    this.categoria = categoria;
                }
                
                public boolean isFamiliaSelected() {
                    return familiaSelected;
                }
                
                public boolean isCategoriasSelected() {
                    return categoriasSelected;
                }
            
                public boolean isCategoriaSelected() {
                    return categoriaSelected;
                }
                
                public Integer getCatId() {
                    return catId;
                }
            
                public void setCatId(final Integer catId) {
                    this.catId = catId;
                }
                
                public String getsFamId() {
                    return sFamId;
                }
            
                public void setsFamId(String sFamId) {
                    this.sFamId = sFamId;
                }
            
                public String getsCatId() {
                    return sCatId;
                }
            
                public void setsCatId(String sCatId) {
                    this.sCatId = sCatId;
                }
            
                public String getNombreFamilia() {
                    return nombreFamilia;
                }
            
                public void setNombreFamilia(final String nombreFamilia) {
                    this.nombreFamilia = nombreFamilia;
                }
            
                public String getNombreCategoria() {
                    return nombreCategoria;
                }
            
                public void setNombreCategoria(final String nombreCategoria) {
                    this.nombreCategoria = nombreCategoria;
                }
            }
            

             

            The xhtml file.-

            (Just only the code related with the bean)

             

            <?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">    
                                    <rich:tab header="Seleccionar Lavado">
                                        <table width="100%" align="center">
                                            <tr><td align="center">
                                                <table align="center">
                                                    <tr valign="middle">
                                                        <td align="center">
                                                            <table align="center">
                                                                <tr valign="middle">
                                                                    <td align="center"><img src="images/catalogo/lavado.png" alt="Lavado" /></td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="center"><p>Repuestos para lavadoras, lavavajillas,<br/>secadoras, lavadoras de turbina...</p></td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="center">
                                                                        
                                                                        <h:form id="formTable">
                                                                            <a4j:outputPanel>
                                                                                <h:panelGrid rendered="#{familiaAgent.familia == null}">    
                                                                                    <a4j:commandButton value="Seleccionar" render="out" action="#{familiaAgent.selectCategorias}" >
                                                                                        <a4j:param name="sFamId" value="1" assignTo="#{familiaAgent.sFamId}" />
                                                                                    </a4j:commandButton>
                                                                                </h:panelGrid>
                                                                            </a4j:outputPanel>
                                            
                                                                            <a4j:outputPanel id="out">
                                                                                <h:panelGrid rendered="#{familiaAgent.categoriasSelected eq true}">
                                                                                    <table align="center">
                                                                                        <tr><td align="center">
                                                                                            
                                                                                            <a4j:status onstart="#{rich:component('statPane')}.show()" onstop="#{rich:component('statPane')}.hide()" />
                                                                                            
                                                                                                <rich:dataTable var="cat" value="#{familiaAgent.categorias}">
                                                                                                    <rich:column>
                                                                                                        <f:facet name="header">Nombre</f:facet>
                                                                                                        <h:outputText value="#{cat.nombre}" />
                                                                                                    </rich:column>
                                                                                                    <rich:column>
                                                                                                        <f:facet name="header">Seleccionar</f:facet>
                                                                                                        <a4j:commandButton render="editGrid" execute="@this"
                                                                                                            value="Seleccionar" action="#{familiaAgent.selectCategoria}"
                                                                                                            oncomplete="#{rich:component('editPane')}.show()" >
                                                                                                              <a4j:param name="sFamId" value="1" assignTo="#{familiaAgent.sFamId}" />
                                                                                                            <a4j:param name="sCatId" value="#{cat.id}" assignTo="#{familiaAgent.sCatId}" />
                                                                                                        </a4j:commandButton>                                                                            
                                                                                                    </rich:column>
                                                                                                </rich:dataTable>
                                                                                                
                                                                                                <rich:popupPanel id="statPane" autosized="true">
                                                                                                    <h:graphicImage value="/images/ai.gif" />
                                                                                                    Please wait...
                                                                                                </rich:popupPanel>
                                                                                                
                                                                                                <br />
                                                                                                
                                                                                                <rich:popupPanel header="Edit Catalogo Details" id="editPane" domElementAttachment="parent" width="250" height="120">
                                                                                                    <h:panelGrid columns="2" id="editGrid">
                                                                                                        <h:outputText value="Id:" />
                                                                                                        <h:outputText value="#{familiaAgent.catId}" />
                                                                                                        
                                                                                                        <h:outputText value="Nombre Familia:" />
                                                                                                        <h:outputText value="#{familiaAgent.nombreFamilia}" />
                                                                                                        
                                                                                                        <h:outputText value="Nombre Categoria:" />
                                                                                                        <h:outputText value="#{familiaAgent.nombreCategoria}" />
                                                                                                    </h:panelGrid>
                                                                                                    <a4j:commandButton value="Seleccionar" action="#{familiaAgent.selectCategoria}"
                                                                                                        render="outCtg" execute="editPane"
                                                                                                        oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('editPane')}.hide();}" >
                                                                                                        <a4j:param name="sFamId" value="1" assignTo="#{familiaAgent.sFamId}" />
                                                                                                        <a4j:param name="sCatId" value="#{familiaAgent.catId}" assignTo="#{familiaAgent.sCatId}" />
                                                                                                    </a4j:commandButton>
                                                                                                    <a4j:commandButton value="Cancel" action="#{familiaAgent.cancel}"
                                                                                                        onclick="#{rich:component('editPane')}.hide(); return false;" />
                                                                                                </rich:popupPanel>                                                                                
                                                                                        </td></tr>
                                                                                    </table>
                                                                                </h:panelGrid>
                                                                            </a4j:outputPanel>
                                                                        </h:form>
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </td></tr>
                                        </table>
                                    </rich:tab>
            

             

            I will study the explanation in Stackoverflow.

             

            Thanks again,

            Jose