I cannot make the LIST page work in JSF

Asked

Viewed 118 times

0

I have the following code I am developing with JAVA JSF, primefaces, postgre, cdi, Tomcat:

BUSCARBEAN

package controller;

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

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import model.basica.RegraExcecao;
import model.basica.Usuario;
import model.fachada.Fachada;

@SuppressWarnings("serial")
@Named("buscaUsuario")
@ViewScoped
public class BuscaUsuarioBean implements Serializable {

private List<Usuario> usuarios = new ArrayList<Usuario>();

@PostConstruct
public void init() {
    try {
        Fachada fachada = new Fachada();
        usuarios = fachada.listarUsuario();
    } catch (RegraExcecao e) {
        e.printStackTrace();
    }
}

public List<Usuario> getUsuarios() {
    try {
        Fachada fachada = new Fachada();
        usuarios = fachada.listarUsuario();
    } catch (RegraExcecao e) {
        e.printStackTrace();
    }
    return usuarios;
}

public void setUsuarios(List<Usuario> usuarios) {
    this.usuarios = usuarios;
    }
}

listar_usuario.xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">

<h:head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Lista de Usuarios</title>

</h:head>

<h:body>
<h:form id="form">
    <p:messages id="messages" showDetail="true" />
    <p:panelGrid columns="2" cellpadding="6">

    <p:dataTable var="usuario" value="#{buscaUsuario.init}">
        <p:column headerText="Id">
            <h:outputText value="#{usuario.id}" />
        </p:column>

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

        <p:column headerText="Login">
            <h:outputText value="#{usuario.login}" />
        </p:column>

        <p:column headerText="Senha">
            <h:outputText value="#{usuario.senha}" />
        </p:column>

        <p:column headerText="Data de Cadastro do Usuário">
            <h:outputText value="#{usuario.dataCadastro}" />
        </p:column>

        <p:column headerText="Data de Alteração do Usuário">
            <h:outputText value="#{usuario.dataAlteracao}" />
        </p:column>

        <p:column headerText="Último acesso do usuário">
            <h:outputText value="#{usuario.ultimoLogin}" />
        </p:column>
    </p:dataTable>

    </p:panelGrid>
</h:form>
</h:body>
</html>

I have already tested in a main test class if the data entered in the database are coming back and are.

I made a

List<Usuario> usuarios = new ArrayList<>();
fachada.listarUsuario();

and shows that they are returning.

Someone would know what I’m doing wrong?

  • Try to change the value="#{buscaUsuario.init}" for value="#{buscaUsuario.usuarios}"

  • Hello Articuno. I did it, but still does not list anything.

2 answers

1

I got it!!! = DDD

The problem was that in another Managedbean I had a list(), so I believe he was not able to find because of this. It was only I referenced it in the list xhtml that caught it! = D

  • Congratulations Annie! Also post the code changed because it may be someone else’s doubt.

  • I didn’t change anything in the code, I just deleted a Bean class where it repeated to list user.

  • Thank you Marcus! =)

-1

I think the problem is value="#{buscaUsuario.init}.

You have put in value a method that returns nothing as a response and so the data is not shown on the page.

Instead change it to value="#{buscaUsuario.usuarios}, so the page will show the list items.

  • I did it already Rubens, but you’re still not listing =/

  • Appears on the screen "no Records found."

Browser other questions tagged

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