How to update a selectonemenu depending on other one selected value?

前端 未结 4 1975
刺人心
刺人心 2021-01-06 10:06

I\'ve been having a trouble trying to make my selectOneMenu content, depend on the value selected on the other. The content from the first one comes from a tabl

4条回答
  •  青春惊慌失措
    2021-01-06 10:45

    I know a solution for RichFaces, I'm pretty sure it will works for PrimeFaces as components are almost the same :

    http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=ajax&sample=selectsUpdates&skin=blueSky

    EDIT : Here is the translation applied to your code :

    
      
        
          
        
    
    
    
        
          
              
        
    
    

    And for the bean :

    @ManagedBean(name = "beanInscripcion") @RequestScoped public class BeanInscripcion implements Serializable {

    static String strURL;
    private List estados; 
    private List municipios;
    private int id_estado;
    public BeanInscripcion() throws SQLException{
            estados = new ArrayList();
            buscarEstados();
    }
    
    public void buscarEstados() throws SQLException {
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery("SELECT * FROM estado");
        result.beforeFirst();
        while (result.next()) {
            Estado estado = new Estado();
            estado.setId_estado(result.getInt("id_estado"));
            estado.setNombre_estado(result.getString("nombre_estado"));
            estados.add(estado);
        }
    }
    
    public void buscarMunicipios() throws SQLException {
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery("SELECT id_municipio, nombre_municipio FROM municipio WHERE Estado_id_estado = '" + id_estado + "'");
        result.beforeFirst();
        while (result.next()) {
            Municipio municipio = new Municipio();
            municipio.setId_municipio(result.getInt("id_municipio"));
            municipio.setNombre_municipio(result.getString("nombre_municipio"));
            municipios.add(municipio);
        }
    }
    
    public Connection getConnection() {
        try {
            strURL = "jdbc:mysql://localhost:3306/mydb";
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection(strURL, "root", "root");
        } catch (SQLException ex) {
            return null;
        } catch (ClassNotFoundException ex) {
            return null;
        }
    }
    
    public List getEstados() {
        return estados;
    }
    
    public void setEstados(List estados) {
        this.estados = estados;
    }
    
    public List getMunicipios() {
        return municipios;
    }
    
    public void setMunicipios(List municipios) {
        this.municipios = municipios;
    }
    
    public int getId_estado() {
        return id_estado;
    }
    
    public void setId_estado(int id_estado) {
        this.id_estado = id_estado;
    }
    

    }

    Hope it will work since it is not tested!

提交回复
热议问题