Conversion Error - Null Converter

Asked

Viewed 2,713 times

3

I have an application where you have a client record with selection of plan, such plan comes from the bank.

when submitting comes the following error: Erro de conversão ao definir o valor 'Plano{id=2, nome=3 MEGA, rateDown=12121, rateUp=2121, valorMensal=2121.0}' para 'null Converter'.

@Entity
public class Login{
    @OneToOne(cascade = CascadeType.ALL)
    Plano plano;
    // getter, setters e outros atributos
}

View

 <p:tab id="contato" title="Internet">
        <p:panel header="Informações de Internet e Plano">
        <p:messages />
            <h:panelGrid columns="2" columnClasses="label, value" rendered="#{clienteMB.cliente.pessoa eq 'FISICA'}">
                               <p:dataTable id="tabelaLogin" value="#{clienteMB.cliente.login}" var="login">
    <p:column headerText="Login">
        <h:panelGrid columns="2" columnClasses="label, value">
            <h:outputText value="Usuário: *" />
           <h:inputText value="#{login.user}"/>
           <h:outputText value="Senha: *" />
            <h:inputText value="#{login.senha}"/>

            <h:outputText value="Plano: *" />
            <p:selectOneMenu id="pessoaslc" value="#{login.plano}">
                <f:selectItems value="#{planoMB.findAll()}" var="plano" itemLabel="#{plano.nome}" itemValue="#{plano}"/>
                </p:selectOneMenu>
        </h:panelGrid>
     </p:column>

   </p:dataTable>
    <p:commandButton value="Add Login" actionListener="#{clienteMB.addNaLista()}" process="@this" update="tabelaLogin"/>

            </h:panelGrid>
        </p:panel>
    </p:tab>

Bean

@Named
@SessionScoped
public class ClienteMB implements Serializable {

@Inject
private ClienteService service;

@Inject
private Cliente cliente;

private Long idSelecionado;

private List<Cliente> clientes;

public ClienteMB() {
}

public void setIdSelecionado(Long idSelecionado) {
    this.idSelecionado = idSelecionado;
}

public Long getIdSelecionado() {
    return idSelecionado;
}

public Cliente getCliente() {
    return cliente;
}

    public void addNaLista() {
        cliente.addLogin(new Login());
    }


public void editar() {
    if (idSelecionado == null) {
        return;
    }
    cliente = service.find(idSelecionado);
    //log.debug("Pronto pra editar");
}

public List<Cliente> getClientes() {
    System.out.println("service: "+service);
    if (clientes == null) {
        clientes = service.findAll();
    }
    return clientes;
}


public String salvar() {
    try {
        System.out.println("service: "+service);
        service.save(cliente);
    } catch(Exception ex) {
        //log.error("Erro ao salvar mercadoria.", ex);
        addMessage(getMessageFromI18N("msg.erro.salvar.cliente"), ex.getMessage());
        return "";
    }
    //log.debug("Salvour mercadoria "+mercadoria.getId());
    return "listaMercadorias";
}

public String remover() {
    try {
        service.remove(cliente);
    } catch(Exception ex) {
        //log.error("Erro ao remover mercadoria.", ex);
        addMessage(getMessageFromI18N("msg.erro.remover.cliente"), ex.getMessage());
        return "";
    }
    //log.debug("Removeu mercadoria "+mercadoria.getId());
    return "listaMercadorias";
}


private String getMessageFromI18N(String key) {
    ResourceBundle bundle = ResourceBundle.getBundle("messages_labels", getCurrentInstance().getViewRoot().getLocale());
    return bundle.getString(key);
}


private void addMessage(String summary, String detail) {
    getCurrentInstance().addMessage(null, new FacesMessage(summary, summary.concat("<br/>").concat(detail)));
}

    public List<Cargo> getCargo(){
        return Arrays.asList(Cargo.values());
    }

    public List<Pessoa> getPessoa() {
        return Arrays.asList(Pessoa.values());  
     }

    public List<Estado> getEstados() {
        return Arrays.asList(Estado.values());
     }
    public void test(){
        System.out.println(cliente);
    }


}

Planomb

@Named
@ConversationScoped
public class PlanoMB implements Serializable{

@Inject
private PlanoService service;

@Inject
private Plano plano;

public Plano getPlano() {
    return plano;
}

public void setPlano(Plano plano) {
    this.plano = plano;
}

public List<Plano> findAll(){
    return service.findAll();
}

public String salvar() {
    try {
        System.out.println("service: "+service);
        service.save(plano);
    } catch(Exception ex) {
        //log.error("Erro ao salvar mercadoria.", ex);
        addMessage(getMessageFromI18N("msg.erro.salvar.plano"), ex.getMessage());
        return "";
    }
    //log.debug("Salvour mercadoria "+mercadoria.getId());
    return "novoplano";
}

    private String getMessageFromI18N(String key) {
    ResourceBundle bundle = ResourceBundle.getBundle("messages_labels", getCurrentInstance().getViewRoot().getLocale());
    return bundle.getString(key);
}


private void addMessage(String summary, String detail) {
    getCurrentInstance().addMessage(null, new FacesMessage(summary, summary.concat("<br/>").concat(detail)));
}

I wonder what’s causing this problem?

1 answer

1


When using a list of Objects in selectOneMenu it is necessary to create a Converter for him. That Converter will convert the itemValue of the item selected for a Plan object (in your case), example of a Converter:

@FacesConverter(value = "planoConverter", forClass = Plano.class)
public class PlanoConverter implements javax.faces.convert.Converter {

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        if (string != null && !string.isEmpty()) {
            return seuMetodoParaProcurarPlanosPorIDnoBanco(string);
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        if (o != null && (o instanceof Plano)) {
            return String.valueOf(((Plano) o).getId());
        }

        return null;
    }

}

and alters its selectOneMenu:

<p:selectOneMenu id="pessoaslc" value="#{login.plano}" converter="planoConverter">
           <f:selectItems value="#{planoMB.findAll()}" var="plano" itemLabel="#{plano.nome}" itemValue="#{plano}"/>
</p:selectOneMenu>

In the showcase of Primefaces has another example of convert, follows the link.

Browser other questions tagged

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