5

selectOneMenu選択した値に応じて、コンテンツを作成するのに問題があります。最初のコンテンツはデータベースのテーブルからのものであり、完全に機能しますが、2番目のコンテンツは別のテーブルからのものであるはずですが、機能させることができません。これが私index.htmlのです。これがどのように機能するかを証明しようとしています。


        <h:outputLabel value="Estado" styleClass="requiredLbl"/>
        <p:selectOneMenu id="Estado" value="#{beanInscripcion.id_estado}" valueChangeListener="#{beanInscripcion.buscarMunicipios(event)}" >  
            <f:selectItem itemLabel="Elegir Estado" itemValue="" />
            <f:selectItems value="#{beanInscripcion.estados}"  
                           var="edo" itemLabel="#{edo.nombre_estado}" itemValue="#{edo.id_estado}" />  
            <p:ajax update="Municipio"  listener="#{beanInscripcion.buscarMunicipios(event)}" />
        </p:selectOneMenu> 
        <p:separator /> 
        <h:outputLabel value="Municipio" styleClass="requiredLbl"/>
        <p:selectOneMenu id="Municipio" value="municipio">  
            <f:selectItems value="#{beanInscripcion.municipios}"  
                           var="mun" itemLabel="#{mun.nombre_municipio}" itemValue="#{mun.nombre_municipio}" />  
        </p:selectOneMenu>

そして、これが私のBeanのセクションで、2番目のメニューのコンテンツを取得することになっています。


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

    static String strURL;
    private List<Estado> estados; 
    private List<Municipio> municipios;
    private int id_estado;
    public BeanInscripcion() throws SQLException{
            estados = new ArrayList<Estado>();
            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<Estado> getEstados() {
        return estados;
    }

    public void setEstados(List<Estado> estados) {
        this.estados = estados;
    }

    public List<Municipio> getMunicipios() {
        return municipios;
    }

    public void setMunicipios(List<Municipio> municipios) {
        this.municipios = municipios;
    }

    public int getId_estado() {
        return id_estado;
    }

    public void setId_estado(int id_estado) {
        this.id_estado = id_estado;
    }
}

私はこれに何時間も取り組んできましたが、まだ何もありません。私はここで本当に急いでいます。ここで助けていただければ幸いです。あなたの注意のためにたくさんのThx:D

4

4 に答える 4

6
  1. value="municipio"in<p:selectOneMenu id="Municipio" value="municipio">は、そのフィールドの値を常に変更するように効果的にハードコーディングしたため、そのドロップダウンの値が決して変更されないことを意味しますmunicipio(それでも変換に失敗します)。value属性は、次のようにバッキング Bean 変数にバインドする必要があります。

      <p:selectOneMenu id="Municipio" value="#{beanInscripcion.municipio}" >  
        <f:selectItems value="#{beanInscripcion.municipios}"  
                       var="mun" itemLabel="#{mun.nombre_municipio}" itemValue="#{mun.nombre_municipio}" />  
    </p:selectOneMenu>
    

    バッキングBeanには、

       Municipio municipio;
      //getter and setter
    
  2. eventからパラメータを削除します<p:ajax update="Municipio" listener="#{beanInscripcion.buscarMunicipios(event)}" />。そのはず

    <p:ajax update="Municipio"  listener="#{beanInscripcion.buscarMunicipios}" />
    
  3. を削除しvalueChangeListener="#{beanInscripcion.buscarMunicipios(event)}"ます。<p:ajax/>すでにイベントが定義されているため不要です

  4. Municipioそのカスタム タイプの JSF コンバーターを作成していないため、最終的にそのフォームの送信で問題が発生します。String選択したコンポーネントでタイプ以外のものを使用している場合は必須です。ここでコンバーター/変換の簡単な紹介を参照してください

于 2012-11-12T06:18:32.790 に答える
5

私は Primefaces を使用していますが、これは非常に簡単です。私は 2 つの seleceOneMenus を持っています。

<h:selectOneMenu id="subject" value="#{QuestionsMB.selectedSubjectId}" var="selectedSubject">  
                <f:selectItem itemLabel="Select Subject" noSelectionOption="true" />
                <f:selectItems value="#{SubjectsMB.subjectsList}" var="selectedSubject" itemValue="#{selectedSubject.id}" itemLabel="#{selectedSubject.subjectName}" />
                <p:ajax update="topic" />
            </h:selectOneMenu>


            <h:selectOneMenu id="topic" value="#{QuestionsMB.selectedTopicId}" var="selectedTopic">  
                <f:selectItem itemLabel="Select Topic" noSelectionOption="true" />
                <f:selectItems value="#{TopicsMB.getTopicsListBySubjectId(QuestionsMB.selectedSubjectId)}" var="selectedTopic" itemValue="#{selectedTopic.id}" itemLabel="#{selectedTopic.topicName}" />
            </h:selectOneMenu>

件名の selectonemenu が変更されると、件名 ID に従ってトピック メニューが変更されることに注意してください。次のように hibernate getList() 関数を編集して、トピックマネージド Bean (子 selectonemenu のマネージド Bean) 内に単純な関数を作成しました。

public List<Topics> getTopicsListBySubjectId(String subjectID) 
    {
        Topics topic = new Topics();
        List<Topics> TopicsList = new ArrayList<Topics>();

        if(subjectID.length() != 0)
        {
            topic.setSubjectId(Integer.parseInt(subjectID));
            TopicsList = getTopicsService().getTopicsBySubjectId(topic);
        }
        else
        {   
            TopicsList.addAll(getTopicsService().getTopics());
        }
        return TopicsList;
    }

そして、物事は魅力のように機能しました.... :)

于 2013-01-22T18:00:04.243 に答える
0

これは単なる例です。プログラムを参照してみてください。

example.xhtml

        <p:selectOneMenu value="#{SelectOneMenuBean.selectedValue}" 
                         style="width:195px;" required="true" id="countryMenu">
            <p:ajax update="cityMenu"/>
            <f:selectItems value="#{SelectOneMenuBean.countries}"/>
        </p:selectOneMenu>
        <p:selectOneMenu style="width:195px;" required="true" id="cityMenu">
            <f:selectItems value="#{SelectOneMenuBean.cities}"/>
        </p:selectOneMenu>

SelectOneMenuBean.java

@ManagedBean(name="SelectOneMenuBean")
public class SelectOneMenuBean {
    private String selectedValue;

    public String getSelectedValue() {
        return selectedValue;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public String[] getCountries() {
        return new String[] {"AAA", "BBB", "CCC", "DDD"};
    }

    public String[] getCities() {
        if(selectedValue != null && selectedValue.equals("AAA")) {
            return new String[] {"City_1 of AAA", "City_2 of AAA", "City_3 of AAA"};
        } else if(selectedValue != null && selectedValue.equals("BBB")) {
            return new String[] {"City_1 of BBB", "City_2 of BBB", "City_3 of BBB"};
        } else if(selectedValue != null && selectedValue.equals("CCC")) {
            return new String[] {"City_1 of CCC", "City_2 of CCC", "City_3 of CCC"};
        } else if(selectedValue != null && selectedValue.equals("CCC")) {
            return new String[] {"City_1 of CCC", "City_2 of CCC", "City_3 of CCC"};
        } 
        return new String[] {"No City"};
    }
}
于 2012-11-12T06:17:27.117 に答える
0

私は RichFaces の解決策を知っています。コンポーネントはほとんど同じであるため、PrimeFaces でも​​機能すると確信しています。

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

編集: コードに適用される翻訳は次のとおりです。

<h:outputLabel value="Estado" styleClass="requiredLbl"/>
<p:selectOneMenu id="Estado" value="#{beanInscripcion.id_estado}" valueChangeListener="#{beanInscripcion.buscarMunicipios(event)}" >  
    <f:selectItem itemLabel="Elegir Estado" itemValue="" />
    <f:selectItems value="#{beanInscripcion.estados}" var="edo" itemLabel="#{edo.nombre_estado}" itemValue="#{edo.id_estado}" />  
    <p:ajax update="second" process="@this" listener="#{beanInscripcion.buscarMunicipios(event)}" />
</p:selectOneMenu>
<p:separator />
<p:outputPanel id="second">
    <h:outputLabel rendered="#{not empty beanInscripcion.id_estado}" value="Municipio" styleClass="requiredLbl"/>
    <p:selectOneMenu rendered="#{not empty beanInscripcion.id_estado}" id="Municipio" value="municipio">  
        <f:selectItems value="#{beanInscripcion.municipios}" var="mun" itemLabel="#{mun.nombre_municipio}" itemValue="#{mun.nombre_municipio}" />  
    </p:selectOneMenu>
</p:outputPanel>

そして豆の場合:

@ManagedBean(name = "beanInscripcion") @RequestScoped パブリック クラス BeanInscripcion は Serializable を実装します {

static String strURL;
private List<Estado> estados; 
private List<Municipio> municipios;
private int id_estado;
public BeanInscripcion() throws SQLException{
        estados = new ArrayList<Estado>();
        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<Estado> getEstados() {
    return estados;
}

public void setEstados(List<Estado> estados) {
    this.estados = estados;
}

public List<Municipio> getMunicipios() {
    return municipios;
}

public void setMunicipios(List<Municipio> municipios) {
    this.municipios = municipios;
}

public int getId_estado() {
    return id_estado;
}

public void setId_estado(int id_estado) {
    this.id_estado = id_estado;
}

}

テストされていないので、うまくいくことを願っています!

于 2012-11-12T05:45:38.143 に答える