根据第一个下拉菜单中的选择更改第二个下拉值

时间:2014-03-25 09:25:17

标签: jsf jsf-2 primefaces

<p:outputLabel id="state" value="State Name" />
    <p:selectOneMenu id="statemenu" style="width:300px;"
            value="#{DropDown.selectedState}">
        <f:selectItem itemLabel="Select One" />
        <f:selectItems value="#{DropDown.stateList}" />
        <p:ajax process="@this" listener="#{DropDown.stateChange}"
        update="dist" partialSubmit="true" />
    </p:selectOneMenu>

<br />

<p:outputLabel value="District" />
<p:selectOneMenu id="dist" style="width:300px;"
        value="#{DropDown.selectedDistrict}" immediate="true">
    <f:selectItem itemLabel="Select One" />
    <f:selectItems value="#{DropDown.districtList}" />
    <p:ajax process="@this" listener="#{DropDown.setDisrict}"
            update="@none" partialSubmit="true" />
</p:selectOneMenu>

这是两个下降。区域的价值在国家的基础上改变。问题是,区域的值被返回为null。即使选择了一个值。

<p:commandButton id="btnSubmit" value="Submit" type="submit"
    process="statemenu,dist,@this"
    actionListener="#{DropDown.setVisiblity}"
    action="#{DropDown.getValues}" title="Submit"
    update=":form:render" partialSubmit="true"
    style='font-family: Baskerville, "Baskerville Old Face", 
        "Hoefler Text", Garamond, "Times New Roman", serif; font-size: 14px; 
        font-weight: normal'>
    <f:ajax execute="@this" render=":form:render" />
</p:commandButton>

这是支持bean:

public class DropDown implements Serializable {

    private ArrayList<String> StateList;
    private ArrayList<String> DistrictList;

    // To store the values of selected state and district from the drop down
    public String SelectedState;
    public String SelectedDistrict;

    public static String State;
    public static String District;

    public DropDown() {
        StateList = new ArrayList<String>();
        StateList = DBConnector.StateList();
    }

    public void setSelectedDistrict(String selectedDistrict) {
        District = selectedDistrict;
        this.SelectedDistrict = selectedDistrict;
    }

    // Method to update the district list when the state is changed
    public void stateChange(AjaxBehaviorEvent event) {
        DistrictList = DBConnector.DistrictList(SelectedState);
    }

    // Method to set the value of district for the datatable
    public void setDisrict(AjaxBehaviorEvent event) {
        System.out.println("District called" + " " + SelectedDistrict);
    }

    public void getValues() {
        System.out.println(State);
        System.out.println(District);
        /*
        * System.out.println(SelectedState);
        * System.out.println(SelectedDistrict);
        */
    }

    public void setVisiblity(ActionEvent event) {
        DataTable.Filter = false;
        DataTable.isLoaded = true;
    }

    // All the getters and setter are exclude

}

一旦我从州菜单中选择州。 dist菜单中的值应该由ajax更新,当我提交值时,selectedstate和selectedDistrict都会反映所选的值。

哪个不行。 selectedstate会重新连接选定的值。但是selectdistrict显示为null。

1 个答案:

答案 0 :(得分:0)

根据您提供的任何代码,

  1. 必须使用xmls或annotations将Bean声明为托管bean。
  2. 将侦听器方法声明更改为

    public void stateChange() { DistrictList = DBConnector.DistrictList(SelectedState); }

  3. 检查DBConnector.DistrictList(SelectedState)是否返回任何内容。还要检查SelectedState是否具有您在下拉列表中选择的正确值。

  4. 此外,最好在班级中使用小写变量。例如。 selectedState代替SelectedState。