Problem implementing JSF Converter with p:selectOneMenu

Asked

Viewed 494 times

0

I’m having problems implementing a convert to a simple selectOneMenu of Primefaces. When you pass the Convert getAsObject method the value parameter comes with the description of what was selected by the user and not with the Id that is in the f:selectItems.

The problem only happens with the Primefaces component, but with the native JSF does not occur.

This is the 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>

inserir a descrição da imagem aqui

The Converter

@FacesConverter(forClass = TipoEvolucao.class)
public class TipoEvolucaoConverter implements Converter {

    @Inject
    TipoEvolucaoRepository repository;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        TipoEvolucao retorno = 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 on the page and the convert will fetch the information in the database by the Id as is default it does not return anything because it can not convert the value to Long.

inserir a descrição da imagem aqui

Has anyone ever had this problem with Primefaces? Even defining the itemValue="#{tipoEvolucao.id}" he always carries the description.

1 answer

0


In itemValue leave only

itemValue="#{tipoEvolucao}"

No need to send the ID, it is already referenced in the convert.

Browser other questions tagged

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