< h:selectOneMenu>为所有下拉列表而不是仅为当前下拉列表调用值更改侦听器 [英] <h:selectOneMenu> value change listener invoked for all dropdowns instead of only the current

查看:101
本文介绍了< h:selectOneMenu>为所有下拉列表而不是仅为当前下拉列表调用值更改侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MyFaces 1.1.我有两个<h:selectOneMenu>下拉菜单,每个下拉菜单都指向相同的valueChangeListener方法.

I'm using MyFaces 1.1. I have two <h:selectOneMenu>s dropdowns which each point to same valueChangeListener method.

<h:selectOneMenu id="d1" value="#{mybean.selectedChannel1}" 
    onchange="submit()" valueChangeListener="#{myform.channelValuechange}">
    <f:selectItems value="#{mybean.channelList}"/>
</h:selectOneMenu>

<h:selectOneMenu id="d2" value="#{mybean.selectedChannel2}"
    onchange="submit()" valueChangeListener="#{myform.channelValuechange}">
    <f:selectItems value="#{mybean.channelList}"/>
</h:selectOneMenu>

当我更改第一个下拉列表时,则正确触发了值更改侦听器方法.在该方法中,我通过ValueChangeEvent参数获取当前组件的ID为sourceId,然后进行如下比较:

When I change the first dropdown, then the value change listener method get fired correctly. In the method, I'm obtaining the ID of the current component as sourceId via ValueChangeEvent argument and then comparing it as follows:

if (sourceId.equals("d1")) {
    // ...
} else if (sourceId.equals("d2")) {
    // ...
}

但是,我的具体问题是,更改d1时也会调用d2块.

However, my concrete problem is that d2 block is also called when d1 is changed.

我尝试了一个,发现以下方法有助于解决该问题:

I tried the one and other and figured that the following helped to solve the problem:

if (!event.getPhaseId().equals(PhaseId.INVOKE_APPLICATION)) {
      event.setPhaseId(PhaseId.INVOKE_APPLICATION);
      event.queue();
}

但是,我不认为这是最好的解决方案.这是怎么引起的?不使用上面的代码怎么解决?

However, I don't feel like that it's the best solution. How is this caused and how can I solve it without using the above code?

推荐答案

使用onchange="submit()",您基本上是在更改当前输入元素时提交 entire 表单,不仅是当前更改的输入!与许多初学者错误地认为的相反,这里没有任何输入特定的JavaScript/Ajax魔术.当您提交整个表单时,它将触发对所有输入组件的处理.

With onchange="submit()" you're basically submitting the entire form when the current input element is changed, not only the currently changed input! In contrary to what many starters incorrectly think, there is no means of any input-specific JavaScript/Ajax magic here. As you're submitting the entire form, it would trigger the processing of all input components.

当输入组件的提交值不像后备bean中那样equals()初始模型值时,始终总是调用valueChangeListener.考虑到您的情况,当您仅更改第一个菜单时,两个菜单均会触发值更改侦听器,这仅意味着第二个菜单的默认选择项值不会equals()后备bean中的初始模型值.

The valueChangeListener is always invoked when the submitted value of the input component does not equals() the initial model value as in the backing bean. Given that in your case both menus hit the value change listener when you change only the first one, that can only mean that the default select item value of the second menu does not equals() the initial model value in the backing bean.

您需要确保默认情况下,第二个菜单的#{mybean.selectedChannel2}与第二个菜单列表的#{mybean.channelList}的第一项具有完全相同的值.这样,当您更改第一个菜单时,不会为第二个菜单调用值更改侦听器.

You need to make sure that #{mybean.selectedChannel2} of the second menu has by default exactly the same value as the first item of #{mybean.channelList} of the second menu's list. This way the value change listener won't be invoked for the second menu when you change the first menu.

这篇关于&lt; h:selectOneMenu&gt;为所有下拉列表而不是仅为当前下拉列表调用值更改侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆