Why can not I load static data in JSF dataTable?

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>
    
asked by anonymous 11.05.2015 / 17:56

2 answers

1

It would be interesting to create a method and annotate it with @postconstruct. So it will run automatically after the bean is injected. I'd rather use this method to "initialize" anything, rather than using the ManagedBean constructor.

@postconstruct
public void init() {
   produtosFiltrados = new ArrayList<>();  
   for (int i = 0; i < 50; i++) {
      produtosFiltrados.add(i);
   }
}
    
29.11.2016 / 18:35
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>

no

  

h: outputText value="123"

put the created variable name, so ...

  

h: outputText value="product"

I hope it helps.

    
08.01.2016 / 14:02