JSF editable datatable shows no object attributes

Asked

Viewed 89 times

0

I need to do an activity with a JSF list where it is possible to add, edit and delete registered users. I had managed to make the records of new people, but when I started using cellEdit to make the edits in the records, I noticed that none of the attributes of the objects appear in the list besides the ID, and I don’t know what the error in the code is. When testing, I noticed that people are being saved in the list, but nothing appears in the datatable.

Code of xhtml:

<?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:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Lista de pessoas</title>
</h:head>
<h:body>
    <h:form id="form">
    <p:growl id="msgs" showDetail="true"/>
        <h:panelGrid columns="4" cellpadding="5">
            <h:outputLabel for="nome" value="Nome:" style="font-weight:bold" />
            <p:inputText id="nome" value="#{gerenciadorBean.pessoa.nome}" />
            <h:outputLabel for="email" value="Email:" style="font-weight:bold" />
            <p:inputText id="email" value="#{gerenciadorBean.pessoa.email}" />
            <p:commandButton value="Atualizar" action="#{gerenciadorBean.inserir()}" update="form" icon="pi pi-check" />
        </h:panelGrid>

        <p:dataTable id="display" var="pessoa" value="#{gerenciadorBean.pessoas}" editable="true" editMode="cell" widgetVar="cellCars">]
            <p:ajax event="cellEdit" listener="#{gerenciadorBean.onCellEdit}" update=":form:msgs" />
            <p:column headerText="ID">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{pessoa.id}"/></f:facet>
                    <f:facet name="input"><p:inputText  value="#{pessoa.id}"/></f:facet>
                </p:cellEditor>
            </p:column>
            <p:column headerText="Nome">
                <f:facet name="output"><h:outputText value="#{pessoa.nome}"/></f:facet>
                <f:facet name="input"><p:inputText value="#{pessoa.nome}"/></f:facet>
            </p:column>
            <p:column headerText="E-Mail">
                <f:facet name="output"><h:outputText value="#{pessoa.email}"/></f:facet>
                <f:facet name="input"><p:inputText value="#{pessoa.email}"/></f:facet>
            </p:column>
        </p:dataTable>

        <p:contextMenu for="display" widgetVar="cMenu">   
            <p:menuitem value="Edit Cell" icon="pi pi-search" onclick="PF('cellCars').showCellEditor();return false;"/>  
            <p:menuitem value="Hide Menu" icon="pi pi-times" onclick="PF('cMenu').hide()"/>  
        </p:contextMenu> 
    </h:form>    
</h:body>
</html>

Code of the Manager:

package br.senai;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class GerenciadorBean {
    
    int id = 1;
    
    private List<Pessoa> listPessoas = new ArrayList();
    
    private Pessoa pessoa = new Pessoa();
    
    public void inserir(){
        pessoa.setId(id);
        listPessoas.add(pessoa);
        pessoa = new Pessoa();
        id++;
    }
    
    public Pessoa getPessoa(){
        return pessoa;
    }
    
    public List<Pessoa> getPessoas(){
        return listPessoas;
    }
    
    public void setPessoa(Pessoa pessoa){
        this.pessoa = pessoa;
    }
}

Code of the Person.java:

package br.senai;

public class Pessoa {
    private String nome;
    private String email;
    private int id;

    public Pessoa(String nome, String email){
        this.nome = nome;
        this.email = email;
        this.id = id;
    }

    public Pessoa(){}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    /*  <h:panelGrid columns="2" cellpadding="5" id="display">
                <h:outputText value="#{gerenciadorBean.exibirNome()}" />
                <h:outputText value="#{gerenciadorBean.exibirEmail()}" />
            </h:panelGrid> */
   }

I started now in JSF and do not know what to do. Any help is welcome!

1 answer

0


Silly mistake! Only the ID appeared because it was the only one inside the tag , and so it was the only one that printed on the table.

Browser other questions tagged

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