Primefaces datatable não exibe os dados [Resolvido]

3 respostas
B

Olá

Já pesquisei muito e não encontrei solução… estou desenvolvendo usando JSF 2, Spring 3, Hibernate, e Primefaces 3.2 … O que acontece é que a <p:datatable não chama o método get das propriedades, assim se meu list tiver 3 elementos ela ira imprimir 3 linhas vazias… muito estranho… segue o código:

ManagedBean (não esta 100%, fiz muitas modificações nele para tentar resolver o problema)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.volitivo.bean;

import br.com.volitivo.dao.ParametroDao;
import br.com.volitivo.entities.Parametro;
import br.com.volitivo.utils.FacesUtils;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@ManagedBean
@SessionScoped
public class ParametroBean {

    private DataModel model;
    private Parametro parametro;
    private ParametroDao dao;

    public ParametroBean() {
        novo();
    }

    @Autowired
    public void setParametroDao(ParametroDao dao) {
        this.dao = dao;
    }

    public String novo() {
        parametro = new Parametro();
        return "admParametro";
    }

    public String salvar() {
        if (!parametro.getChave().isEmpty() && !parametro.getValor().isEmpty()) {
            try {
                if (dao == null) {
                    FacesUtils.mensErro("dao null");
                }
                dao.salvarOuAtualizar(parametro);
                novo();
            } catch (Exception e) {
                e.printStackTrace();
                FacesUtils.mensErro("Erro ao salva parâmetro");
            }
        } else {
            FacesUtils.mensErro("Parâmetro com dados incorretos");
        }
        return "admParametro";
    }

    public List<Parametro> getTodos2() {
        List<Parametro> l = new ArrayList<Parametro>();
        Parametro p = new Parametro();
        p.setChave("c");
        p.setValor("vl");
        l.add(p);
        return l;
    }

    public DataModel getModel() {
        return model;
    }

    public void setModel(DataModel model) {
        this.model = model;
    }

    public Parametro getParametro() {
        return parametro;
    }

    public void setParametro(Parametro parametro) {
        this.parametro = parametro;
    }
}

Página de teste

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>TODO supply a title</title>
    </h:head>
    <h:body>
        <p:dataTable value="#{parametroBean.todos2}" var="param">

             <!-- Isso imprime -->
            <p:column headerText="Chave">
                <h:outputText value="{param.chave}" />
            </p:column>

            <!-- Isso não funciona -->
            <p:column headerText="Valor">
                <h:outputText value="#{param.valor}" />
            </p:column>
        </p:dataTable>
    </h:body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <!-- Suporte ao spring -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
  
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
  
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
        <!--        <param-value>Production</param-value>-->
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>panel/geral.jsf</welcome-file>
    </welcome-file-list>
</web-app>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
    <navigation-rule>
        <from-view-id>/panel/*</from-view-id>
        <navigation-case>
            <from-outcome>admParametro</from-outcome>
            <to-view-id>/panel/cadtParametro.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>geral</from-outcome>
            <to-view-id>/panel/geral.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

Esse é meu primeiro projeto usando Spring, então pode ser que ele esteja relacionado com isso rsrsrsrs
Alguém sabe o que esta errado/pode me ajudar?

3 Respostas

adornes

Cara, bem estranho mesmo…

Imagino que os getters getChave() e getValor() estejam ok, senão daria um erro bem mais feio. Mas tenta, talvez, mudar o nome da variável no teu dataTable. Em vez de “param” tenta só ‘p’ ou algo do tipo. Talvez o componente esteja se perdendo com o nome dessa variável. Sei lá. Problema estranho, sugestão estranha, hehe.

Abraço!

B

Nããããããããoooooooo acredito, funcionou… Obrigado Ardones :smiley:

Pocha, bem que o primefaces podia ter dado um “feedback” se as coisas não estavam como ele queria hehehe

adornes

Hahaha, nem eu acredito.

É, todo programador passa por esse tipo de coisa. hehe

Boa sorte na continuidade dos trabalhos!

Abraço

Criado 6 de maio de 2012
Ultima resposta 6 de mai. de 2012
Respostas 3
Participantes 2