即使我没有更改值,也会在selectOneMenu上调用valueChangeListener方法 [英] valueChangeListener method is called on selectOneMenu even though I have not changed the value
问题描述
我有以下问题.当我单击按钮"Enviar"时,这将调用与selectOneMenu关联的另一个方法(在属性中
valueChangeListener称为"validarSelect"),然后再调用此按钮在名为"validarBoton"的属性actionListener中关联的方法.
我想知道为什么会这样.我希望不会调用valueChangeListener
,因为我没有更改下拉菜单.
I have the following problem. When I click the button "Enviar", this calls another method that is associated to a selectOneMenu (in the attribute
valueChangeListener called "validarSelect"), and later, calls the method that this button has associated in the attribute actionListener called "validarBoton".
I wonder, why this happens. I expect the valueChangeListener
to be not called since I have not changed the dropdown.
这是我的页面JSF:
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<html xmlns="http://www.w3.org/1999/xhtml">
<h:head></h:head>
<h:body>
<h:form>
<h:commandButton value="Enviar..." id="validar" actionListener="#{Domiciliacion.validarBoton}"/>
<h:selectOneMenu valueChangeListener="#{Domiciliacion.validarSelect}"
binding="#{Domiciliacion.selectCombo}">
<f:selectItems value="#{Domiciliacion.lista}"/>
<f:ajax event="valueChange" render="@this"/>
</h:selectOneMenu>
</h:form>
</h:body>
</html>
这是ManagedBean:
And this, is the ManagedBean:
package domiciliaciontest;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
@ManagedBean(name = "Domiciliacion")
@ViewScoped
public class MB0001 {
private HtmlSelectOneMenu selectCombo;
private List<String> lista = new ArrayList<String>();
public MB0001() {
super();
System.out.println("Entro al constructor...");
lista.add("Caracas");
lista.add("Bogota");
lista.add("Santiago");
}
public void validarBoton(ActionEvent actionEvent) {
System.out.println("Entro a validarBoton...");
// Add event code here...
}
public void validarSelect(ValueChangeEvent valueChangeEvent) {
// Add event code here...
System.out.println("Entro a validarSelect...");
}
public void setSelectCombo(HtmlSelectOneMenu selectCombo) {
this.selectCombo = selectCombo;
}
public HtmlSelectOneMenu getSelectCombo() {
return selectCombo;
}
public void setLista(List<String> lista) {
this.lista = lista;
}
public List<String> getLista() {
return lista;
}
}
这是我单击"Enviar"按钮时的输出:
this is the output when I click the button "Enviar":
- 输入有效的ararSelect ...
- 输入一个有效的Boton ...
推荐答案
提交的值与初始值不同时,将调用valueChangeListener
方法,无论您是否已对其进行了更改您自己还是不.因此,如果当前提交的值(在您的情况下为"Caracas"
)与初始值(在您的情况下为null
)不同,则将调用valueChangeListener
方法.
The valueChangeListener
method will be invoked when the submitted value is different from the initial value, regardless of whether you have changed it yourself or not. So, if the currently submitted value (which is "Caracas"
in your case) is different from the initial value (which is null
in your case), then the valueChangeListener
method will be invoked.
- When to use valueChangeListener or f:ajax listener?
- Best way to add a "nothing selected" option to a selectOneMenu in JSF
无关与具体问题相关,将其与binding
属性结合使用给我的印象是,您正在尝试实现针对JSF的文章或答案中已阅读的内容1.x.也就是说,这是在JSF 1.x中填充子下拉列表的黑客的一部分.对于JSF 2.x,您不需要需要这种方法.此外,带有"validar"("validate")的方法名称具有误导性.您实际上不是真的需要一个足够的Validator
吗?但是正如所说的,这是一个不同的问题.
Unrelated to the concrete problem, seeing this in combination with binding
attribute gives me the impression that you're trying to achieve something which you've read in an article or answer targeted on JSF 1.x. This is namely recognizeable as part of a hack to populate child dropdowns in JSF 1.x. You do not need this approach for JSF 2.x. Further, your method names with "validar" ("validate") are misleading. Don't you actually need a fullworthy Validator
? But as said, that's a different problem.
这篇关于即使我没有更改值,也会在selectOneMenu上调用valueChangeListener方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!