I'm having trouble implementing a convert to a simple selectOneMenu of Primefaces. When passing the getAsObject method of the Convert value parameter comes with the description of what was selected by the user and not with the Id that is in the itemValue of f:selectItems
.
The problem only happens with the Primefaces component, but with the JSF native it does not occur.
This is SelectOneMenu:
<p:selectOneMenu id="tipoEvolucao" value="#{atendimentoBean.atendimento.tipoEvolucao}" effect="fold">
<f:selectItems value="#{atendimentoBean.tipoEvolucaoList}" itemValue="#{tipoEvolucao.id}"
var="tipoEvolucao" itemLabel="#{tipoEvolucao.descricao}" />
</p:selectOneMenu>
TheConverter
@FacesConverter(forClass=TipoEvolucao.class)publicclassTipoEvolucaoConverterimplementsConverter{@InjectTipoEvolucaoRepositoryrepository;@OverridepublicObjectgetAsObject(FacesContextcontext,UIComponentcomponent,Stringvalue){TipoEvolucaoretorno=null;if(value!=null&&!"".equals(value)){
try {
Long id = new Long(value);
retorno = repository.getById(id);
} catch (Exception e) {
return retorno;
}
}
return retorno;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null && value.getClass().getGenericSuperclass() == GenericEntity.class){
TipoEvolucao entity = ((TipoEvolucao) value);
return entity.getId() == null ? null : entity.getId().toString();
}
return null;
}
}
With this whenever I choose a value in the on page and the converter will fetch the information in the database by Id as it is standard it returns nothing by nor can convert the value to Long.
HasanyoneeverhadthisproblemwithPrimefaces?EvensettingtheitemValue="#{tipoEvolucao.id}"
it always takes the description.