Capture the change event in a p:selectOneenum type

Asked

Viewed 731 times

0

I cannot capture the change event of the p:selectOneMenu component, whose upload source is an Enum. I registered in my view the Ajax change event, 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>

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 to the Component System does not print the information on the console, i.e., 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 run.

1 answer

0


Well, the capture of the change event in a component p:selectOneMenu does not differ in any way from the way in which it was implemented by the author of the question: it’s that simple. So, if there is no problem in the way you are intercepting the event, it follows that the failure is at another point, or at another part of the code that involves this operation.

The reason for the failure

After a few days with a lot of anger at not being able to identify the problem and finding me a donkey so I discovered that the fault was related to a poorly implemented Setter. That’s what 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 Converter: Genericenumconverter

The Omnifaces is a project developed by Bauke Scholtz, the 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 solve code redundancy I adopted Omnifaces. One of the classes provided by the project is the class Genericenumconverter which provides a generic converter for an Enum, thus removing the need to create a converter for each Enum in my project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.