Convert called more than once in a p:selectOneMenu ajax

UI Components for JSF
Post Reply
mariohmol
Posts: 14
Joined: 26 Mar 2011, 21:08

30 Jul 2011, 20:02

Hy guys,

i'm trying to use a p:selectOneMenu pointing to a converter and calling a ajax to populate another p:selectOneMenu.

But on debug mode i can see that the Converter has been calling 3 times in a sigle post!!
At the first time it sends the Id and it convertes to a valid Object, but in the others send a empty String.

Take a look at the code:

1. Above 2 p:selectOneMenu , where estado populate cidades.

Code: Select all

 <h:outputLabel value="#{bundle.Estado}:" for="estado" />
                        <p:selectOneMenu value="#{usuariosMBean.selectedEstado}" id="estado" converter="#{estadosConverter}" >
                            <f:selectItem itemLabel="#{bundle.SelecioneCombo}" itemValue="" />
                            <f:selectItems value="#{usuariosMBean.estados}" var="estado" itemLabel="#{estado.nome}"
                                           itemValue="#{estado.id}"  />
                            <p:ajax event="change" immediate="true"  update="cidade" />
                        </p:selectOneMenu>
                        <p:message for="estado" />

                        <h:outputLabel value="Cidade:" for="cidade" />
                        <p:selectOneMenu value="#{usuariosMBean.selectedCidade}" id="cidade" converter="#{cidadeConverter}" >
                            <f:selectItem itemLabel="#{bundle.SelecioneCombo}" itemValue="" />
                            <f:selectItems value="#{usuariosMBean.cidades}" var="cidade" itemLabel="#{cidade.nome}"
                                           itemValue="#{cidade.id}" />
                        </p:selectOneMenu>
                        <p:message for="cidade" />

2. This is my converter, take a look that i'm using a converter as a MB to get the @EJB functionality

Code: Select all

@ManagedBean(name = "cidadeConverter")
@RequestScoped
public class CidadeConverter extends BaseConverter {

      @EJB
    private EnderecosSessionBean enderecosSessionBean;

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
        try {
            Integer id = Integer.parseInt(submittedValue);
            if (submittedValue != null) {

                Cidades cidadeResulta = enderecosSessionBean.getCidadesById(id);
                if(cidadeResulta!=null) return cidadeResulta;
            }
        } catch (Exception e) {
        }
        return new Cidades();
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        if (value != null && value instanceof Cidades) {
            Cidades ben = (Cidades) value;
            if (ben.getId() != null) {
                return ben.getId().toString();
            }
        }
        return "";
    }
}
3. There is not a error on log, not even a warn. I can see on the debug mode that the estado property has not been set #{usuariosMBean.selectedEstado}, above the code's at usuariosMBean

Code: Select all

//Putting a breakpoint above is possible to see that this setSelectedEstado is never been calling, even after the Converter breakpoint
    public void setSelectedEstado(Estados selectedEstado) {
        this.selectedEstado = selectedEstado;
    }

 public Estados getSelectedEstado() {
        return selectedEstado;
    }

 public List<Estados> getEstados() {
        if (estados == null) {
            estados = enderecosSessionBean.getEstadosByPais(null);
        }
        return estados;
    }

 public List<Cidades> getCidades() {

        return selectedCidades;
    }
 public Cidades getSelectedCidade() {
        return selectedCidade;
    }

    public void setSelectedCidade(Cidades selectedCidade) {
        this.selectedCidade = selectedCidade;
    }
4. I'm using GF3.1 + Primefaces 3.0M2

The workaround that worked was take the converter out and use a ID attribute instead of Estade Class on p:selectOneMenu.

My doubt is why the converter is been calling more than once and why the selSelectedEstado has been called?

Regards,

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

31 Jul 2011, 07:18

Converter is called for each item during rendering.

mariohmol
Posts: 14
Joined: 26 Mar 2011, 21:08

31 Jul 2011, 07:25

But I have just one component in xhtml using this Converter.

Did I undesrtand right what you said?

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

31 Jul 2011, 08:05

How many items do you display, it doesn't matter how many components you have?

Do you understand me right?

mariohmol
Posts: 14
Joined: 26 Mar 2011, 21:08

31 Jul 2011, 15:29

In this test I used just these 2 components and just 1 component using this converter.

And other important point is: In the debug mode the function setSelectedEstado has not been called.

Regards,

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 49 guests