Capture the change event in a p: selectOneMenu of type Enum

0

I can not catch the change event of component p: selectOneMenu, whose load source is an enum. I registered in my view the change event of Ajax, however the listener method is not called.

My view uses the following code to load the combo:

<p:selectOneMenu id="analise" value="#{fooMB.analise}" converter="AnaliseConverter">
    <f:selectItem itemLabel="Selecione"/>
    <f:selectItems value="#{fooMB.carregarComboAnalise}" var="analise" itemLabel="#{analise.descricao}" itemValue="#{analise}/>
    <f:ajax event="change" listener="#{fooMB.changeComboAnalise}"/>
</p:selectOneMenu>

The enum has the following code:

public enum AnaliseEnum {

    AVALIACAO("Avaliação"), 
    SUGESTAO("Sugestão"), 
    RECLAMACAO("Reclamação"), 
    OUTROS("Outros");

    private String descricao;

    private AnaliseEnum(String descricao) {
        this.descricao = descricao;
    }

    public String getDescricao() {
        return this.descricao;
    }
}

My ManagedBean has the following method used to load the combo:

public AnaliseEnum[] carregarComboAnalise() {
    return  AnaliseEnum.values();
}

My converter has the following code:

@FacesConverter(value = "AnaliseConverter")
public class AnaliseConverter extends EnumConverter {

    public AnaliseConverter() {
        super(AnaliseEnum.class);
    }
}

The method added in the component's listener does not print the information on the console, that is, it is not triggered.

public void changeComboAnalise(AjaxBehaviorEvent event) {
    System.out.println("Deveria fazer alguma coisa aqui, mas não faz .... ");
}

The combo loads normally, but the changeComboAnalise () method called in the "change" event does not execute.

    
asked by anonymous 10.04.2015 / 20:55

1 answer

0

Well, catching the change event on a p:selectOneMenu component does not differ in anything from the way it was implemented by the question author: it's that simple . So if there is no problem in how the event is being intercepted, it is assumed that the failure is at another point, or another part of the code that involves this operation.

The reason for the failure

After a few days of being very angry at not being able to identify the problem and finding myself a fool for it, I discovered that the fault was related to a poorly implemented setter. That's right you read, a poorly implemented setter. My setter method was not passing the parameter as it does in the example below:

public void setAnalise(AnaliseEnum analise) {
    this.analise = analise;
}

Generic Enum: GenericEnumConverter

OmniFaces is a project developed by Bauke Scholtz, BalusC , and Arjan Tijms . It is a utility library that provides a set of classes, components, in order to facilitate development with JSF.

As a way to resolve code redundancy I adopted OmniFaces. One of the classes provided by the project is the GenericEnumConverter class that provides a generic Enum converter, thus removing the need to create one converter for each Enum of my project.

    
11.04.2015 / 18:57