Help with JSF Login System

Asked

Viewed 75 times

-1

I am creating a jsf login system, which I validate the data through a bean and if(has record) redirects to the home page, Lse goes to the error page, but always enters Else, I found that the object used in DAO carries beauty, however, when I make the conditions in my bean to redirect (or not) my object is empty (as print below), I did not find the why, someone can help me ?

Print in the Console of how values are being returned in the bean and DAO inserir a descrição da imagem aqui

bean:

//todos os importes feitos
public class pessoaMB {

private Pessoa pessoa;
private PessoaDAO pDAO = new PessoaDAO();

public pessoaMB() {
    pessoa = new Pessoa();
}

public String doLogin() {
    SimpleHash hash = new SimpleHash("md5",pessoa.getSenha());
    pDAO.BuscarLogin(pessoa.getCpf(), hash.toHex());
    System.out.println("Pessoa recebida - BEAN");
    System.out.println("Id-----------"+pessoa.getId());
    System.out.println("Nome---------"+pessoa.getNome());
    System.out.println("Nível--------"+pessoa.getNivel());
    if(pessoa.getId()>0) {
        HttpSession hs = Sessao.getSession();
        hs.setAttribute("usuario", pessoa.getNome());
        return "/pages/principais/principalDiscente.xhtml?faces-redirect=true";
    }
    else {
        FacesMessage fm = new FacesMessage("Login error","ERRO MSG");
        fm.setSeverity(FacesMessage.SEVERITY_ERROR);
        FacesContext.getCurrentInstance().addMessage(null, fm);
        return "/pages/principais/erroLogin.xhtml?faces-redirect=true";
    }
}

public String doLogout() {
    HttpSession hs = Sessao.getSession();
    hs.invalidate();


    return "/index.xhtml?faces-redirect=true";
    }
//getters e setter feitos também

DAO

public class PessoaDAO {
    public List<Pessoa> BuscarLogin(String cpf, String senha) {


Connection con = Conexao.getConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    List<Pessoa> pessoa = new ArrayList<>();
    try {
        stmt = con.prepareStatement("SELECT id,nome,nivel FROM pessoa where cpf = ? and senha = ?");
        stmt.setString(1, cpf);
        stmt.setString(2, senha);
        rs = stmt.executeQuery();
        while (rs.next()) {
            Pessoa p = new Pessoa();
            p.setId(rs.getInt("id"));
            p.setNome(rs.getString("nome"));
            p.setNivel(rs.getInt("nivel"));
            pessoa.add(p);
            System.out.println("--------BUSCA NO DAO--------");
            System.out.println("Query----"+stmt);
            System.out.println("Id--------"+p.getId());
            System.out.println("Nome------"+p.getNome());
            System.out.println("Nivel-----"+p.getNivel());
            System.out.println("----------------------------");
        }

    } catch (SQLException ex) {
        Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        Conexao.closeConnection(con, stmt, rs);
    }


       return pessoa;
    }
}

CANVAS:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:adm="http://github.com/adminfaces"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
    </h:head>

    <h:body styleClass="hold-transition login-page">
        <div class="login-box">
            <div class="box login-box-body">
                <h:form>
                    <div align="center"><h1>SGE</h1></div>
                    <p class="login-box-msg">Entre com seus dados para acessar o sistema</p>
                    <p:messages closable="true"/>
                    <div class="form-group has-feedback">
                        <p:inputMask value="#{PessoaMB.pessoa.cpf}" styleClass="form-control" placeholder="Digite seu CPF" required="true" requiredMessage="Favor preencher o campo cpf." mask="999.999.999-99"/>
                    </div>
                    <div class="form-group has-feedback">
                        <p:inputText value="#{PessoaMB.pessoa.senha}" type="password" styleClass="form-control" placeholder="Digite sua senha" required="true" requiredMessage="Favor preencher o campo senha"/>
                    </div>
                    <div class="row">
                        <p:spacer height="10"/>
                        <div class="col-xs-12">
                            <p:commandButton styleClass="btn btn-success btn-block" action="#{PessoaMB.doLogin}" value="Entrar" ajax="false"/>
                        </div>
                    </div>
                </h:form>
            </div>
            <!-- /.login-box-body -->
        </div>
        <h:outputScript library="js" name="admintemplate.js" target="head"/>
    </h:body>
</html>
  • Solved with: public String doLogin() {&#xA; SimpleHash hash = new SimpleHash("md5",pessoa.getSenha());&#xA; Pessoa pnova = pDAO.BuscarLogin(pessoa.getCpf(), hash.toHex());&#xA; if(pessoa.getId()>0) {&#xA; HttpSession hs = Sessao.getSession();&#xA; hs.setAttribute("usuario", pnova.getNome());&#xA; Return "/main/main pages/principalDiscent.xhtml? faces-redirect=true"; } Else { Facesmessage fm = new Facesmessage("Login error","MSG ERROR"); } }

  • puts your answer as a response and you mark as a response after you mark as the solution

1 answer

0


Resolved with:

public String doLogin() { 
SimpleHash hash = new SimpleHash("md5",pessoa.getSenha()); 
Pessoa pnova = pDAO.BuscarLogin(pessoa.getCpf(), hash.toHex()); if(pessoa.getId()>0) { 
HttpSession hs = Sessao.getSession(); 
hs.setAttribute("usuario", pnova.getNome()); 
return "/pages/principais/principalDiscente.xhtml?faces-redirect=true"; 
} else { 
FacesMessage fm = new FacesMessage("Login error","ERRO MSG");
} } 
  • What was the problem? Why did this snippet of code solve?

  • The problem is higher, just read, this code solved because I need to store the query in an object, so I did: Person pnova = pDAO.Buscarlogin() and solved my problem

Browser other questions tagged

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