Datatable data is not displayed, even with loading of Arraylist running correctly

Asked

Viewed 99 times

1

Guys, I am loading a JSF datatable with an Arraylist, but the result is not displayed in any way, follow as my code:
index.xhtml

<h:body>
    <h1>Novo Jogo</h1>
    <j:form>
        <fieldset>
            <legend>Dados do Jogo</legend>
            <j:panelGrid columns="2">
                <j:outputLabel value="Titulo:" for="titulo" />
                <j:inputText id="titulo" value="#{meusJogosBean.mj.titulo}" />
                <j:outputLabel value="Desenvolvedora:" for="desenvolvedora" />
                <j:inputText id="desenvolvedora"
                    value="#{meusJogosBean.mj.desenvolvedora}" />
                <j:outputLabel value="Ano Lancamento:" for="ano_lancamento" />
                <j:inputText id="ano_lancamento"
                    value="#{meusJogosBean.mj.ano_lancamento}" />
                <j:outputLabel value="plataforma:" for="plataforma" />
                <j:inputText id="plataforma" value="#{meusJogosBean.mj.plataforma}" />
                <j:commandButton value="Gravar" action="#{meusJogosBean.gravar}">
                </j:commandButton>
            </j:panelGrid>
        </fieldset>
    </j:form>
<ui:define name="conteudo">
    <h:form id="formTabelaJogos">
        <p:dataTable value="#{meusJogosBean.jogos}" var="j" id="tabelaJogos" lazy="true">
                <p:column headerText="Título" >
                    <h:outputText value="#{j.titulo}"/>
                    <h:outputText value="text" />
                </p:column>
    </p:dataTable>
    </h:form>
</ui:define>

</h:body>
</html>


Meusjogosbean.java

package model;


import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import model.MeusJogos;
import model.DAO;

@ManagedBean
@ViewScoped
public class MeusJogosBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private MeusJogos mj = new MeusJogos();

    public List<MeusJogos> getJogos() {
        return new DAO<MeusJogos>(MeusJogos.class).listaTodos();
    }

    public MeusJogos getMj() {
        return mj;
    }

    public void setMj(MeusJogos mj) {
        this.mj = mj;
    }

    public void gravar() {
        System.out.println("Gravando jogo: " + this.mj.getTitulo());
        new DAO<MeusJogos>(MeusJogos.class).adiciona(mj);

    }


}

Meusgames.Java

package model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class MeusJogos {
    @Id @GeneratedValue
    private Integer id;
    private String titulo;
    private String desenvolvedora;
    private String ano_lancamento;
    private String plataforma;

    public MeusJogos() {

    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitulo() {
        return titulo;
    }
    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }
    public String getDesenvolvedora() {
        return desenvolvedora;
    }
    public void setDesenvolvedora(String desenvolvedora) {
        this.desenvolvedora = desenvolvedora;
    }
    public String getAno_lancamento() {
        return ano_lancamento;
    }
    public void setAno_lancamento(String ano_lancamento) {
        this.ano_lancamento = ano_lancamento;
    }
    public String getPlataforma() {
        return plataforma;
    }
    public void setPlataforma(String plataforma) {
        this.plataforma = plataforma;
    }

}

DAO.java

package model;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;

public class DAO<T> {

    private final Class<T> classe;

    public DAO(Class<T> classe) {
        this.classe = classe;
    }

    public void adiciona(T t) {

        // consegue a entity manager
        EntityManager em = new JPAUtil().getEntityManager();

        // abre transacao
        em.getTransaction().begin();

        // persiste o objeto
        em.persist(t);

        // commita a transacao
        em.getTransaction().commit();

        // fecha a entity manager
        em.close();
    }

    public void remove(T t) {
        EntityManager em = new JPAUtil().getEntityManager();
        em.getTransaction().begin();

        em.remove(em.merge(t));

        em.getTransaction().commit();
        em.close();
    }

    public void atualiza(T t) {
        EntityManager em = new JPAUtil().getEntityManager();
        em.getTransaction().begin();

        em.merge(t);

        em.getTransaction().commit();
        em.close();
    }

    public List<T> listaTodos() {
        EntityManager em = new JPAUtil().getEntityManager();
        CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
        query.select(query.from(classe));

        List<T> lista = em.createQuery(query).getResultList();

        System.out.println(lista.size());
        em.close();
        return lista;

    }

    public T buscaPorId(Integer id) {
        EntityManager em = new JPAUtil().getEntityManager();
        T instancia = em.find(classe, id);
        em.close();
        return instancia;
    }

    public int contaTodos() {
        EntityManager em = new JPAUtil().getEntityManager();
        long result = (Long) em.createQuery("select count(n) from jogo n")
                .getSingleResult();
        em.close();

        return (int) result;
    }

    public List<T> listaTodosPaginada(int firstResult, int maxResults) {
        EntityManager em = new JPAUtil().getEntityManager();
        CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
        query.select(query.from(classe));

        List<T> lista = em.createQuery(query).setFirstResult(firstResult)
                .setMaxResults(maxResults).getResultList();

        em.close();
        return lista;
    }

}
  • What error does the IDE report? Post the error!!

2 answers

1


If you are using the attribute lazy="true" it is necessary to process data modeling with the class LazyDataModel.

The attribute lazy makes the pagination "real", that is, makes each change of the pages of the datatable make a new request until the back end, arriving to the bank with at least two information: the index of the line from which the data must be returned and the amount of data to be returned. For example: if you have a query that results in 100 records and your pagination in the datatable is 10 items per page, when the user clicks on the second page the information 11 and 10 will be sent, being the 11 the index from where to return the data and the 10 the amount of data to be returned.

The lazy when we are working with a considerable database. For few records sometimes (repeat, sometimes) it is better to leave the pagination "false", where at the time of loading the datatable, it is already loaded in full and the pagination is only in the view.


If you want to use the lazy, you need to create a class that inherits from LazyDataModel<T>. For example:

import org.primefaces.model.LazyDataModel;

public class LazyJogosDataModel extends LazyDataModel<MeusJogos> { }

In this class you will need to overwrite (at least) the method load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters). (If I am not mistaken there is an overload of this method that receives only the first two parameters)

At first it is a little complicated/boring to understand how it works. But after you get the hang of it is quiet.

I strongly advise you to take a look in this example on the site of Primefaces. He is very good and well complete.

1

Do not use List when using Lazy = true. puts it like this:

LazyDataModel<MeusJogos>  = new LazyDataModel(){

  public load (...){
    DAO<MeusJogos>(MeusJogos.class).listaTodos();

  }
}

will solve your problem

Browser other questions tagged

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