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" />
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 "";
}
}
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;
}
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,