Problems loading datatable Primefaces

Asked

Viewed 896 times

0

I am using in the Java Web CDI, JPA and Maven application, my application is already entering records in the database without any problem, now what I have left is list the records in the Primefaces datatable.

As I am a programmer with little experience I put a static list to then slowly immigrate to a list loaded from the database as shown in the figure below.

inserir a descrição da imagem aqui

but when I go to immigrate the list trying to load from the database I am not successful

my bean class got like this

package com.algaworks.carrinho.controller;

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

import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import com.algaworks.carrinho.modelo.Produto;
import com.algaworks.carrinho.repository.Produtos;

@Named
@ViewScoped
public class ListaProdutoBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Inject
    private Produtos produtos;

    private List<Produto> produtosFiltrados;

    public ListaProdutoBean(){
        this.produtosFiltrados = new ArrayList<Produto>();
    }

    public List<Produto> getProdutos(){

        return this.produtosFiltrados = this.produtos.getFindAll();
    }

    public List<Produto> getProdutosFiltrados() {
        return produtosFiltrados;
    }

    public void setProdutos(Produtos produtos) {
        this.produtos = produtos;
    }

    public void setProdutosFiltrados(List<Produto> produtosFiltrados) {
        this.produtosFiltrados = produtosFiltrados;
    }





}

My xhtml file looked like this

<ui:composition template="/WEB-INF/template/LayoutPadrao.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

    <ui:define name="titulo">Dashboard</ui:define>
    <ui:define name="corpo">

        <h:form>
            <p:messages autoUpdate="true" closable="true" />

            <p:toolbar>
                <p:toolbarGroup align="left">
                    <p:commandButton value="Salvar" id="botaoSalvar"
                        action="#{cadastroProdutoBean.salvar}" update="@form" />

                </p:toolbarGroup>
            </p:toolbar>

            <p:panelGrid columns="2" id="panel">

                <p:outputLabel value="Nome" for="nome" />
                <p:inputText id="nome" size="50"
                    value="#{cadastroProdutoBean.produto.nome}" />

                <p:outputLabel value="Descrição" for="descricao" />
                <p:inputText id="descricao" size="100"
                    value="#{cadastroProdutoBean.produto.descricao}" />

                <p:outputLabel value="Preço" for="preco" />
                <p:inputText id="preco" size="50"
                    value="#{cadastroProdutoBean.produto.preco}" />

                <p:outputLabel value="Quantidade" for="qtd" />
                <p:inputText id="qtd" size="10"
                    value="#{cadastroProdutoBean.produto.quantidade}" />


            </p:panelGrid>
        </h:form>
        <p:separator style="margin-top: 20px" />

        <h:form>
            <p:dataTable id="produtoTable"
                value="#{listaProdutoBean.produtosFiltrados}" var="produto"
                rows="20">

                <p:column headerText="Nome">
                    <h:outputText value="#{produto.nome}" />
                </p:column>


                <p:column headerText="Descrição">
                    <h:outputText value="#{produto.descricao}" />
                </p:column>


                <p:column headerText="Preço">
                    <h:outputText value="#{produto.preco}" />
                </p:column>


                <p:column headerText="Quantidade">
                    <h:outputText value="#{produto.quantidade}" />
                </p:column>



            </p:dataTable>


        </h:form>
    </ui:define>

</ui:composition>

I have to create a way that my bean class was correct to load the removed list from the bank.

here is the link of the last version of the modifications I made in the application

https://github.com/wladyband/Carrinho/tree/master/Carrinho

  • It gets kind of hard with this error message, like, says there was a problem but doesn’t point out what it is.

  • I changed the entire post, and there is still something missing to solve the problem just do not know what it is, could you take a look at the modifications I made in my post.

1 answer

1


In the datatable, you used the method: value="#{listProdutoBean.productsFiltrados}" that in the Managedbean code only returns the variable, if you use the getProducts() method you will run findAll.

Since you included the code on github, I took the liberty of using your project’s Pojo class to generate an application with the Demoiselle Framework (http://demoiselle.sourceforge.net/docs/framework/reference/2.4.2/html/), using the automatic code generation tool Demoiselle Nimble: http://demoiselle.sourceforge.net/docs/tools/nimble/1.2.0/html/

The result is here: https://github.com/esaito/ExemplosDemoiselle/tree/master/carrinho

It may be an option to decrease some of your work.

The application is ready to run on the Jboss 7.1 server, already using the embedded database (H2) of the same server.

The Demoiselle project offers pre-configured environment installation packages that can facilitate development, see here: http://demoiselle.sourceforge.net/infra/2.0.0/userguide.html

Browser other questions tagged

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