0 Replies Latest reply on Aug 17, 2007 11:43 AM by lusp00ky

    Problem using 2 selectOneMenu's with 2 managed beans and aja

    lusp00ky

      Hi,

      I have a person registration form with 2 selectOneMenu. Select a item in the first will load itens in the second. Since I want to reuse this 2 selectOneMenu in a company registration form, I have two managed beans (CidadeMBean and EstadoMBean) for the selects, and another bean for the form (PessoaMBean).

      The cycle should be:
      1. select "estado" in the first select
      2. set in the PessoaBean the attribute "estado"
      3. call ajax4jsf and pass "estado" to the other bean
      4. using this "estado", load the list for the second
      5. load the second select

      But i'm having problems, the above code isn't working at all. I already tried actionParam but it didn't work out. I couldn't find examples using that kind of interaction, all of the examples I saw use the same bean for the form and the selects.

      I'm using ajax4jsf 1.1.1 - there is a bug with 1.1.0 with actionParam.

      code from the form:

       <h:selectOneMenu id="estado" value="#{pessoaMBean.pessoa.estado}" styleClass="selectComum" style="width:45px;">
       <f:selectItems value="#{estadoMBean.comboEstados}" />
       <a4j:support event="onchange" action="#{cidadeMBean.load}" ajaxSingle="true" reRender="cidade"/>
       </h:selectOneMenu>
      
      
       <h:selectOneMenu id="cidade" value="#{pessoaMBean.pessoa.cidade.id}" styleClass="selectComum" style="width:45px;">
       <f:selectItems value="#{cidadeMBean.comboCidades}" />
       </h:selectOneMenu>
      


      form's bean
      public class PessoaMBean {
      
       private PessoaService pessoaService;
       private Pessoa pessoa;
       private DataModel model;
      
       private static final Logger log = Logger.getLogger(PessoaMBean.class);
      
       public PessoaMBean() {
       }
      
       public String save() {
       this.pessoaService.save(pessoa);
       return "list";
       }
      
       public String create() {
       this.pessoa = new Pessoa();
       return "new";
       }
      
       public String view() {
       this.pessoa = (Pessoa) model.getRowData();
       return "view";
       }
      
       public Pessoa getPessoa() {
       return pessoa;
       }
      
       public void setPessoa(Pessoa pessoa) {
       this.pessoa = pessoa;
       }
      
       public DataModel getPessoas() {
       model = new ListDataModel(pessoaService.getListPessoa());
       return model;
       }
      
       public PessoaService getPessoaService() {
       return pessoaService;
       }
      
       public void setPessoaService(PessoaService pessoaService) {
       this.pessoaService = pessoaService;
       }
      }
      


      first select mbean
      public class EstadoMBean {
      
       private List<SelectItem> comboEstados;
       private List<Estado> estados;
       private EstadoDAO estadoDAO;
      
       public List<Estado> getEstados() {
       if (this.estados == null) this.estados = estadoDAO.getListEstados();
       return estados;
       }
      
       public void setEstados(List<Estado> estados) {
       this.estados = estados;
       }
      
       public List<SelectItem> getComboEstados() {
       if (this.comboEstados == null) this.comboEstados = estadoDAO.getSelectListEstados();
       return comboEstados;
       }
      
       public void setComboEstados(List<SelectItem> comboEstados) {
       this.comboEstados = comboEstados;
       }
      
       public EstadoDAO getEstadoDAO() {
       return estadoDAO;
       }
      
       public void setEstadoDAO(EstadoDAO estadoDAO) {
       this.estadoDAO = estadoDAO;
       }
      
      }
      


      the second select mbean
      public class CidadeMBean {
      
       private List<SelectItem> comboCidades;
       private CidadeDAO cidadeDAO;
      
       // this must be set with the selected "estado" !!!!
       private Estado estado;
      
       private static final Logger log = Logger.getLogger(CidadeMBean.class);
      
       public void load() {
       if (estado != null) this.comboCidades = cidadeDAO.getSelectListCidadesByEstado(estado);
       else return new ArrayList<SelectItem>();
       }
      
       public List<SelectItem> getComboCidades() {
       load();
       return comboCidades;
       }
      
       public void setComboCidades(List<SelectItem> comboCidades) {
       this.comboCidades = comboCidades;
       }
      
      
       public Estado getEstado() {
       return estado;
       }
      
       public void setEstado(Estado estado) {
       this.estado = estado;
       }
      
       public CidadeDAO getCidadeDAO() {
       return cidadeDAO;
       }
      
       public void setCidadeDAO(CidadeDAO cidadeDAO) {
       this.cidadeDAO = cidadeDAO;
       }
      }
      


      my faces-beans.xml
      <?xml version="1.0"?>
      <!DOCTYPE faces-config PUBLIC
       "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
       "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
      <faces-config>
       <!-- Spring with JSf -->
       <application>
       <variable-resolver>
       org.springframework.web.jsf.DelegatingVariableResolver
       </variable-resolver>
       </application>
      
       <managed-bean>
       <managed-bean-name>pessoaMBean</managed-bean-name>
       <managed-bean-class>br.com.conectait.mbeans.PessoaMBean</managed-bean-class>
       <managed-bean-scope>session</managed-bean-scope>
       <managed-property>
       <property-name>pessoaService</property-name>
       <value>#{pessoaService}</value>
       </managed-property>
       </managed-bean>
      
       <managed-bean>
       <managed-bean-name>cidadeMBean</managed-bean-name>
       <managed-bean-class>br.com.conectait.mbeans.CidadeMBean</managed-bean-class>
       <managed-bean-scope>session</managed-bean-scope>
       <managed-property>
       <property-name>cidadeDAO</property-name>
       <value>#{cidadeDAO}</value>
       </managed-property>
       </managed-bean>
      
       <managed-bean>
       <managed-bean-name>estadoMBean</managed-bean-name>
       <managed-bean-class>br.com.conectait.mbeans.EstadoMBean</managed-bean-class>
       <managed-bean-scope>session</managed-bean-scope>
       <managed-property>
       <property-name>estadoDAO</property-name>
       <value>#{estadoDAO}</value>
       </managed-property>
       </managed-bean>
      </faces-config>
      




      Any ideas, suggestion, reference to solve this issue?

      Thanks in advance!