5

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.

This is my page 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>

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

this is the output when I click the button "Enviar":

  • Entro a validarSelect...
  • Entro a validarBoton...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Carlos
  • 65
  • 1
  • 6
  • 1
    That's specified behaviour. What's your question/problem? Is that not what you want? Elaborate the concrete functional requirement in detail then. By the way, you do not want to use `binding` this way, I'd entirely remove it, but that's another problem. – BalusC Apr 16 '12 at 21:49
  • Hi BalusC, but shouldn't it just call the actionListener when I click on the button? – Carlos Apr 17 '12 at 13:20
  • since I'm not changing the value of the h:selectOnMenu – Carlos Apr 17 '12 at 13:27

1 Answers1

5

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.

See also:


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.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • mmm Ok, and how i should changed the value of SelectOneMenu to initialize ? is said, of null by other personalized value ? Thank you !!! – Carlos Apr 17 '12 at 16:27
  • Just set the component's `value` to the desired value. If you replace `binding="#{Domiciliacion.selectCombo}"` by `value="#{Domiciliacion.selectCombo}"` with `private String selectCombo;` then just add the line `selectCombo = "Caracas";` to the bean's constructor. – BalusC Apr 17 '12 at 16:31
  • As said, just set the component's `value` to the desired value. Go back through your basic JSF book/tutorial (or go find/buy one if you don't already have one). – BalusC Apr 17 '12 at 16:34