p:selectOneMenu的监听器只能部分工作

时间:2016-10-17 04:40:06

标签: jsf primefaces jsf-2

我有一个简单的JSF表单,有三个下拉列表, 这是代码:

<h:form>
            <p:panel header="Select country" style="margin-bottom:10px;">
                <h:panelGrid columns="2" cellpadding="5">
                    <p:outputLabel for="country" value="Country: " />
                    <p:selectOneMenu id="country" value="#{phaseOneController.country}" style="width:150px">
                        <p:ajax listener="#{phaseOneController.onCountryChange}" update="province" />
                        <f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
                        <f:selectItems value="#{phaseOneController.countries}" />
                    </p:selectOneMenu>

                    <p:outputLabel for="province" value="Province: " />
                    <p:selectOneMenu id="province" value="#{phaseOneController.province}" style="width:150px">
                        <p:ajax listener="#{phaseOneController.onProvinceChange}" update="city" />
                        <f:selectItem itemLabel="Select Province" itemValue="" noSelectionOption="true" />
                        <f:selectItems value="#{phaseOneController.provinces}" />
                    </p:selectOneMenu>

                <p:outputLabel for="city" value="City: " />
                <p:selectOneMenu id="city" value="#{phaseOneController.city}" style="width:150px">
                    <f:selectItem itemLabel="Select City" itemValue="" noSelectionOption="true" />
                    <f:selectItems value="#{phaseOneController.cities}" />
                </p:selectOneMenu>
            </h:panelGrid>

                <p:separator />

                <p:commandButton value="Select" actionListener="#{phaseOneController.submit}" icon="ui-icon-check">
                </p:commandButton>
            </p:panel>
        </h:form>

现在,当我选择一个国家时,onCountryChange会被调用,但是当我更新省份时,onProvinceChange根本就没有被调用!!令人惊讶的是,一个人会如何工作而另一个人不会

以下是控制器的代码:

public void onCountryChange() {
        System.out.println("On country change called");
    }

    public void onProvinceChange() {
        System.out.println("On province change called");
    }

为了缩短问题,我已经替换了侦听器方法中的代码。

答案:For anyone who faces this issue, using HashMap to populate the dropdown solved the issue. I am not sure why list is giving so much trouble.

2 个答案:

答案 0 :(得分:0)

尝试添加event =&#34;更改&#34; ...

      <p:selectOneMenu id="country" value="#{phaseOneController.country}" style="width:150px">
                <p:ajax event="change" listener="#{phaseOneController.onCountryChange}" update="subCategory" />
                <f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
                <f:selectItems value="#{phaseOneController.countries}" />
            </p:selectOneMenu>

                <p:selectOneMenu id="province" value="#{phaseOneController.province}" style="width:150px">
                    <p:ajax  event="change" listener="#{phaseOneController.onProvinceChange}" update="city" />
                    <f:selectItem itemLabel="Select Province" itemValue="" noSelectionOption="true" />
                    <f:selectItems value="#{phaseOneController.provinces}" />
                </p:selectOneMenu>

答案 1 :(得分:0)

@ViewScoped和自定义转换器中的bean放置在视图范围内,每个f:selectItems应解决此问题。

请参阅:

Cascading p:selectOneMenu model value not set