【问题标题】:The listener for p:selectOneMenu working only partiallyp:selectOneMenu 的侦听器仅部分工作
【发布时间】:2016-10-17 04:40:06
【问题描述】:

我有一个简单的 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.

【问题讨论】:

标签: jsf primefaces jsf-2


【解决方案1】:

尝试添加 event="change"...

      <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>

【讨论】:

    【解决方案2】:

    将 bean 通过 @ViewScoped 和自定义转换器放置在视图范围内到每个 f:selectItems 应该可以解决这个问题。

    请看:

    Cascading p:selectOneMenu model value not set

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      相关资源
      最近更新 更多