Error when trying to display the information of an object in a <p: dialog/>

Asked

Viewed 40 times

1

I am developing a system using JSF, Primefaces, JPA, Maven and Postgresql. I’m new to JEE and I’m having difficulties with the use of Primefaces, so let’s get to the problem:

I have a customer listing screen and on this screen I would like to be able to view, update or delete a registered customer.

To start I am trying to display information from a selected client, however, by selecting the object on the screen and clicking the button to display the dialog, I am getting the following exception:

Cannot Convert Client {"here is the information of the selected object :) "} of type class model.entidade.Client to class javax.faces.model.Listdatamodel model

ManagedBean:

@ManagedBean
@SessionScoped
public class ControlerCliente implements Serializable {
    private static final long serialVersionUID = 1L;
    private Cliente cliente;
    private Endereco endereco;
    private ListDataModel<Cliente> selectClientes =null;

 public ControlerCliente() {
        this.endereco = new Endereco();
        this.cliente = new Cliente();
        }
    public List<Cliente> obterClientes() {
        return ClienteModel.getInstanceCliModel().findAllClientesModel();

    }

}

Listing screen:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <body>

        <ui:composition template="./../templates/template.xhtml">

            <ui:define name="top">
                <ui:include src="/includes/menu.xhtml"/>
            </ui:define>

            <ui:define name="content">
                <h:form>
                    <p:dataTable id="singleCli" var="cliente" value="#{controlerCliente.obterClientes()}" rows="3" rowKey="#{cliente.codigo}"
                                 selection="#{controlerCliente.selectClientes}" selectionMode="single" paginator="true">
                        <f:facet name="header">
                            LISTA DE CLIENTES CADASTRADOS
                        </f:facet>

                        <p:column headerText="CODIGO: ">
                            <h:outputLabel value="#{cliente.codigo}"/>
                        </p:column>
                        <p:column headerText="NOME: " filterBy="#{cliente.nome}">
                            <h:outputText value="#{cliente.nome}"/>
                        </p:column>
                        <p:column headerText="CPF: ">
                            <h:outputText value="#{cliente.cpf}"/>
                        </p:column>
                        <p:column headerText="DATA CADASTRO">
                            <h:outputText value="#{cliente.dataAbertura}"/>
                        </p:column>

                        <p:column headerText="E-MAIL: ">
                            <h:outputText value="#{cliente.email}"/>
                        </p:column>

                        <p:column headerText="TELEFONE: ">
                            <h:outputText value="#{cliente.telefone}"/>
                        </p:column>

                        <p:column headerText="RUA: ">
                            <h:outputText value="#{cliente.endereco.bairro}"/>
                        </p:column>

                        <p:column headerText="NÚMERO: ">
                            <h:outputText value="#{cliente.endereco.numero}"/>
                        </p:column>
                        <p:column headerText="CEP: ">
                            <h:outputText value="#{cliente.endereco.cep}"/>
                        </p:column>

                        <p:column headerText="OPÇÕES">
                            <p:commandButton icon="ui-icon-trash"/>                            
                        </p:column>
                        <f:facet name="footer">
                            <p:commandButton value="VISUALIZAR" onclick="PF('cliDialog').show();"/>
                        </f:facet>


                    </p:dataTable>
                </h:form>

                <p:dialog id="visuCliente"  header="cliente - view" widgetVar="cliDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false"
                          draggable="false" >

                    <h:form>
                        <p:panelGrid  columns="2"  >
                            <h:outputText value="CÓDIGO"/>
                            <p:outputLabel value="#{controlerCliente.cliente.codigo}"/>

                            <h:outputText value="CPF"/>
                            <p:outputLabel value="#{controlerCliente.cliente.cpf}"/>

                            <h:outputText value="EMAIL"/>
                            <p:outputLabel value="#{controlerCliente.cliente.email}"/>

                            <h:outputText value="NOME"/>
                            <p:outputLabel value="#{controlerCliente.cliente.nome}"/>

                            <h:outputText value="DATA-CADASTRO"/>
                            <p:outputLabel value="#{cliente.dataAbertura}"/>

                            <h:outputText value="RUA"/>
                            <p:outputLabel value="#{controlerCliente.cliente.endereco.rua}"/>

                            <h:outputText value="BAIRRO"/>
                            <p:outputLabel value="#{controlerCliente.cliente.endereco.cep}"/>

                            <h:outputText value="CIDADE"/>
                            <p:outputLabel value="#{controlerCliente.cliente.endereco.cidade}"/>

                        </p:panelGrid>

                    </h:form>
                </p:dialog>

            </ui:define>


        </ui:composition>

    </body>
</html>

1 answer

1


The datatable has the parameter selectionMode="single", so you can select only one record from the table. The problem is that you are setting the selected record in a Listdatamodel, at: selection="#{controlerCliente.selectClientes}". Try to switch to selection="#{controlerCliente.cliente}".

Browser other questions tagged

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