Problem to convert with composite key into jsf, selectonemenu

Asked

Viewed 84 times

0

I am making a selectOneMenu where the itemvalue will be a composite key. Which will be separated by a hyphen. However, when sending, the getasobject method is called several times, sending all items from my selectonemenu and not just the selected one.

    @FacesConverter(value = "conversorProd")
public class ConversorProdutoDB implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
        if (value != null && !value.isEmpty()) {
            try {
                String[] split = value.split("-"); //assumindo que será separado (idProduto - idGradeSelecionada)

                if (split.length > 1) {
                    GradeProduto gradeProd = PadraoDAO.carregar(GradeProduto.class, Long.valueOf(split[1]));
                    Produto prodComGrade = gradeProd.getProd();
                    prodComGrade.setGradeSelecionada(gradeProd);
                    return prodComGrade;
                } else {
                    return (Produto) PadraoDAO.carregar(Produto.class, Long.valueOf(split[0]));
                }
            } catch (ExceptionBancoDeDados ex) {
                Logger.getLogger(ConversorProdutoDB.class.getName() + "-Erro ao executar conversor de produto").log(Level.SEVERE, null, ex);
            }
        }
        return "";
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {

            return value != null ? value.toString() : "";

    }
}


          <p:selectOneMenu value="#{transferenciaEstoqueMB.transferenciaDet.transProdOrigem.prod}"
                                     filter="true" filterMatchMode="contains"
                                     converter="conversorProd"
                                     id="slcProd"
                                     >
                        <f:selectItem itemLabel="Selecione" noSelectionOption="true"/>
                        <f:selectItems value="#{transferenciaEstoqueMB.listProduto}"
                                       var='p'
                                       itemLabel="#{p.cod}-#{p.descricao}"
                                       itemValue="#{p.idComposta}"/>
                        <p:ajax listener="#{transferenciaEstoqueMB.onChangeProd()}"
                                process="@this"  update="@(.transProd)"/>
                    </p:selectOneMenu>
  • Ever tried to implement in the getAsString converter to pass the composite key ? For the return of Object is the value that is passing this method.

  • is already the composite key. Note the itemvalue. @Mariohayasakijúnior I did this with the simple Product id to test and it worked. It just doesn’t work on the composite key. There to solve...

No answers

Browser other questions tagged

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