Why can’t I upload static data to the JSF datatable?

Asked

Viewed 578 times

1

This is my class:

package com.algaworks.pedidovenda.controller;

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

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

@ManagedBean
@ViewScoped
public class PesquisaProdutosBean implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private List<Integer> produtosFiltrados;

    public PesquisaProdutosBean() {
        produtosFiltrados = new ArrayList<>();  
        for (int i = 0; i < 50; i++) {
            produtosFiltrados.add(i);
        }
    }

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

}

This is the page:

<ui:composition template="/WEB-INF/template/LayoutPadrao.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">Novo Produto</ui:define>

    <ui:define name="corpo">

        <h:form id="frm">
            <h1>Pesquisa Produtos</h1>

            <p:toolbar style="margin-top:20px">
                <p:toolbarGroup>
                    <p:commandButton value="Pesquisa" />
                </p:toolbarGroup>
                <p:toolbarGroup align="right">
                    <p:button value="Novo" outcome="/produtos/CadastroProduto" />
                </p:toolbarGroup>
            </p:toolbar>

            <p:panelGrid columns="2" id="painel"
                style="width:100%; margin-top:20px" columnClasses="rotulo, campo">

                <p:outputLabel value="SKU" for="sku" />
                <p:inputText id="sku" size="20" maxlength="20" />

                <p:outputLabel value="Nome" for="nome" />
                <p:inputText id="nome" size="60" maxlength="80" />
            </p:panelGrid>

            <p:dataTable id="produtosTable"
                value="#{pesquisaProdutosBean.produtosFiltrados}" var="produto">

                <p:column headerText="SKU" style="text-align: center; width: 100px">
                    <h:outputText value="123" />
                </p:column>

            </p:dataTable>

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

</ui:composition>
  • Is any error shown or just isn’t displayed? You confirmed if getProdutosFiltrados is called when rendering the page?

  • Try to change @ViewScoped for @RequestScoped.

  • no error message appears on consoles, simply did not display, and I have tried to change the annotations in the Bean class and it did not work.

  • Is it because of the version of Primefaces and JSF?

2 answers

1

It would be interesting to create a method and annotate it with @postconstruct. So it will run automatically after the bean has been injected. I prefer to use this method to "initialize" anything, instead of using the Managedbean constructor.

@postconstruct
public void init() {
   produtosFiltrados = new ArrayList<>();  
   for (int i = 0; i < 50; i++) {
      produtosFiltrados.add(i);
   }
}

0

wladyband see the code below

<p:dataTable id="produtosTable"
value="#pesquisaProdutosBean.produtosFiltrados}" var="produto">

    <p:column headerText="SKU" style="text-align: center; width: 100px">         
         <h:outputText value="123" />
    </p:column>

</p:dataTable>

in the

h:outputText value="123"

put the name of the created variable, like this...

h:outputText value="product"

hope it helps.

Browser other questions tagged

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