Problema com List em jsf

11 respostas
E

Ola pessoal, eu mais uma vez aqui para pedir ajuda de vocês é o seguinte
em tenho um List com o metodo (getListaPessoa e setListaPessoa )que é exibido em um datatable so jsf, funciona legal, porem
quando no get List eu uso Collections.sort(list) para ordenar ele gera a seguinte exeção javax.servlet.ServletException: java.lang.StackOverflowError

é um list de pessoa

segue minha classe pessoa

public class Pessoa implements Comparable<Pessoa>  {

    private Integer id;
    private String nome;
    private String tipoPessoa;
    private String sexo;
    private String rg;
    private String cpf;
    private String cep;
    private Date dataNascimento;
    private static int geradorId ;

    public Pessoa() {
        geradorId = ++geradorId;
        id= geradorId;
    }

    public Pessoa(int id, String nome, String tipoPessoa, String sexo, String rg, String cpf, String cep, Date dataNascimento) {
        this.id = id;
        this.nome = nome;
        this.tipoPessoa = tipoPessoa;
        this.sexo = sexo;
        this.rg = rg;
        this.cpf = cpf;
        this.cep = cep;
        this.dataNascimento = dataNascimento;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public Date getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(Date dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getRg() {
        return rg;
    }

    public void setRg(String rg) {
        this.rg = rg;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public String getTipoPessoa() {
        return tipoPessoa;
    }

    public void setTipoPessoa(String tipoPessoa) {
        this.tipoPessoa = tipoPessoa;
    }

    @Override
    public boolean equals(Object obj) {
        Pessoa pessoa = (Pessoa) obj;
        if(pessoa.getId() == pessoa.getId()){
            return true;
        }
        if(pessoa.getId() != pessoa.getId()){
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 79 * hash + this.id;
        return hash;
    }

    public int compareTo(Pessoa pessoa) {
        if(pessoa.getId() < pessoa.getId()){
            return -1;
        }
        if(pessoa.getId() > pessoa.getId()){
            return 1;
        }
        return 0;    }


}

meu MbPessoa

public class MbPessoa {

    private Pessoa pessoa;
    private List<Pessoa> listaPessoa;

    //Construtor
    public MbPessoa() {
        pessoa = new Pessoa();
        listaPessoa = new ArrayList<Pessoa>();

    }

    public List<Pessoa> getListaPessoa() {
       Collections.sort(getListaPessoa()); --------------------------------se eu tirar isso funciona
       return listaPessoa;
    }

    public void setListaPessoa(List<Pessoa> listaPessoa) {
        this.listaPessoa = listaPessoa;
    }

    public Pessoa getPessoa() {
        return pessoa;
    }

    public void setPessoa(Pessoa pessoa) {
        this.pessoa = pessoa;
    }


    public String gravar(){
        this.listaPessoa.add(getPessoa());
        this.pessoa = new Pessoa();
        return "";
    }

    public String irAlterar(){
      return "irAlterar";
    }

    public String alterar(){
        boolean existe = listaPessoa.contains(this.pessoa);
        if(existe){
            listaPessoa.remove(this.pessoa);
            listaPessoa.add(this.pessoa);
        }
        return "alterar";
    }
    public String apagarTodos(){
        this.listaPessoa.clear();
        return "";
    }

   
    public String apagar() {
        this.listaPessoa.remove(getPessoa());
        return null;
    }

    public String voltar(){
        return "voltar";
    }

e meu jsf

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%--
    This file is an entry point for JavaServer Faces application.
--%>
<f:view>
    <html>
        <head>
            <style type="text/css">
                .cabecalhoDataTable {
                    background-color:#CAE1FF;
                }
                .titulo {
                    color: black;
                    font-weight:bold;
                }
            </style>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>Cadastro de Clientes</title>
        </head>
        <body>
            <h:panelGrid  border="1"
                          columns="1"
                          width="697"
                          bgcolor="#FFFFF0">
                <h:outputText styleClass="titulo"
                              value="Cadastro de Clientes"/>
            </h:panelGrid>
            <h:panelGrid bgcolor="#FFFFF0"
                         border="1"
                         columns="1">
                <h:form id="frmCadastroCliente">
                    <h:panelGrid border="0"
                                 cellpadding="3"
                                 cellspacing="3"
                                 id="pnlCliente"
                                 columns="4"
                                 width="680">
                        <h:outputLabel for="itxNome"
                                       value="Nome :"/>
                        <h:inputText id="itxNome"
                                     value="#{mbPessoa.pessoa.nome}"
                                     required="true"/>
                        <h:outputLabel for="itxRg"
                                       value="Rg :"/>
                        <h:inputText id="itxRg"
                                     value="#{mbPessoa.pessoa.rg}"
                                     required="true">
                            <f:validateLength maximum="8" 
                                              minimum="8"/>
                        </h:inputText>
                        <h:outputLabel for="itxCpf"
                                       value="Cpf :"/>
                        <h:inputText id="itxCpf"
                                     value="#{mbPessoa.pessoa.cpf}"
                                     required="true">
                            <f:validateLength maximum="11" 
                                              minimum="11"/>
                        </h:inputText>
                        <h:outputLabel for="itxCep"
                                       value="Cep :"/>
                        <h:inputText id="itxCep"
                                     value="#{mbPessoa.pessoa.cep}"
                                     required="true">
                            <f:validateLength maximum="8" 
                                              minimum="8"/>
                        </h:inputText>
                        <h:outputLabel for="somnTpPessoa"
                                       value="Tipo Pessoa :"/>
                        <h:selectOneMenu id="somnTpPessoa"
                                         value="#{mbPessoa.pessoa.tipoPessoa}">
                            <f:selectItem itemLabel="Fisica"
                                          itemValue="Física"/>
                            <f:selectItem itemLabel="Jurídica"
                                          itemValue="Jurídica"/>
                        </h:selectOneMenu>
                        <h:outputLabel for="somnSexo"
                                       value="Sexo : "/>
                        <h:selectOneRadio id="somnSexo"
                                          value="#{mbPessoa.pessoa.sexo}"  required="true">
                            <f:selectItem itemLabel="Masculino"
                                          itemValue="Masculino"/>
                            <f:selectItem itemLabel="Femenino"
                                          itemValue="Femenino"/>
                        </h:selectOneRadio>
                        <h:outputLabel for="itxDataNascimento"
                                       value="Data Nascimento :"/>
                        <h:inputText id="itxDataNascimento"
                                     value="#{mbPessoa.pessoa.dataNascimento}"
                                     required="true">
                            <f:convertDateTime pattern="dd/MM/yyyy" />
                        </h:inputText>
                    </h:panelGrid>
                    <br><br>
                    <h:panelGrid  border="1"
                                  columns="1"
                                  cellpadding="5"
                                  width="687">
                        <h:column>
                            <h:commandButton id="btnVoltar"
                                             value="Voltar"/>
                            &nbsp&nbsp
                            <h:commandButton id="btnSalvar"
                                             value="Gravar"
                                             action="#{mbPessoa.gravar}"/>
                            &nbsp&nbsp
                            <h:commandButton id="btnLimparFormulario"
                                             type="reset"
                                             value="Limpar Formulário"
                                             immediate="true"/>
                        </h:column>
                    </h:panelGrid>
                    <h:panelGrid  width="680">
                        <h:message for="itxNome"
                                   style="color: red"/>
                        <h:message for="itxRg"
                                   style="color: red"/>
                        <h:message for="itxCpf"
                                   style="color: red"/>
                        <h:message for="itxCep"
                                   style="color: red"/>
                        <h:message for="somnSexo"
                                   style="color: red"/>
                        <h:message for="itxDataNascimento"
                                   style="color: red"/>
                        <h:outputText styleClass="titulo" value="Lista de Pessoa"/>
                        <h:dataTable id="tblListaPessoa"
                                     value="#{mbPessoa.listaPessoa}"
                                     var="pessoa"
                                     border="1"
                                     width="680"
                                     headerClass="cabecalhoDataTable"
                                     bgcolor="white">
                            <h:column >
                                <f:facet name="header" >
                                    <h:outputText  style="color:black" value="Id" />
                                </f:facet>
                                <h:outputText  value="#{pessoa.id}"/>
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText style="color:black" value="Nome" />
                                </f:facet>
                                <h:outputText value="#{pessoa.nome}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText style="color:black" value="Rg"/>
                                </f:facet>
                                <h:outputText value="#{pessoa.rg}"/>
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText style="color:black" value="Cpf"/>
                                </f:facet>
                                <h:outputText value="#{pessoa.cpf}"/>
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText style="color:black" value="Cep"/>
                                </f:facet>
                                <h:outputText value="#{pessoa.cep}"/>
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText style="color:black" value="Tipo Pessoa"/>
                                </f:facet>
                                <h:outputText value="#{pessoa.tipoPessoa}"/>
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText style="color:black" value="Sexo"/>
                                </f:facet>
                                <h:outputText value="#{pessoa.sexo}"/>
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText style="color:black" value="Data de Nascimento"/>
                                </f:facet>
                                <h:outputText value="#{pessoa.dataNascimento}">
                                    <f:convertDateTime pattern="dd/MM/yyyy" />
                                </h:outputText>
                            </h:column>
                            <h:column >
                                 <f:facet name="header">
                                    <h:outputText style="color:black"
                                                  value="Apagar Registro"/>
                                </f:facet>
                                <h:commandButton value="Apagar" 
                                                 action="#{mbPessoa.apagar}"
                                                 immediate="true">
                                    <f:setPropertyActionListener target="#{mbPessoa.pessoa}"
                                                                 value="#{pessoa}"/>
                                </h:commandButton>
                            </h:column>
                              <h:column >
                                 <f:facet name="header">
                                    <h:outputText style="color:black" value="Alterar Registro"/>
                                </f:facet>
                                <h:commandButton value="Alterar"
                                                 action="#{mbPessoa.irAlterar}"
                                                 immediate="true">
                                      <f:setPropertyActionListener target="#{mbPessoa.pessoa}"
                                                                 value="#{pessoa}"/>
                                </h:commandButton>
                            </h:column>
                        </h:dataTable>
                        <h:panelGrid border="1"
                                     columns="1"
                                     width="680">
                            <h:column>
                                <h:commandButton id="btnApagarTudo"
                                                 value="Apagar Todos"
                                                 action="#{mbPessoa.apagarTodos}"
                                                 immediate="true">
                                </h:commandButton>
                              </h:column>
                        </h:panelGrid>
                    </h:panelGrid>
                </h:form>
            </h:panelGrid>
        </body>
    </html>
</f:view>

Obrigado

11 Respostas

thiago.correa

Mas o que estava acontecendo era que o método getListaPessoa fica se chamando o que causa o estouro da pilha

Use apenas Collections.sort(listaPessoa);
Use o getters/setters quando estiver fora da classe, não faz sentindo usar os métodos se você tem acesso aos atributos, a não ser que tenha alguma lógica neles.

E

thiago.correa
era isso mesmo cara valeu obrigado…

mais não está ordenando alguma sugestão?

thiago.correa

Tua classe tem que implementar o método compareTo da interface Comparable!

E

Olha minha classe Pessoa la em cima, está implementando ja

thiago.correa

Olha o jeito que tu implementou!
Não seria

public int compareTo(Pessoa pessoa) {   
        return id - pessoa.getId();    
    }
E

compareTo pensei que sua logica fosse o seguinte
-1 objeto é menor
0 ele é igual
1 ele é maior

não seria essa a logica?

thiago.correa

A lógia é essa mas concorda comigo que pessoa.getId() - pessoa.getId() sempre vai ser 0?!

Você compara o objeto corrente com o que está sendo passado como parâmetro!

E

deste jeito funcionou mesmo soh não itendi sua logica =S , sempre tinha implementado do outro jeito

E

poderia me explicar melhor?

thiago.correa

Cara, a explicação tá bem enxuta!
Dá uma lida nisso aqui e vê se te ajuda

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

E

Blz valews

Criado 13 de abril de 2010
Ultima resposta 13 de abr. de 2010
Respostas 11
Participantes 2