-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
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() {
 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 "/main/main pages/principalDiscent.xhtml? faces-redirect=true"; } Else { Facesmessage fm = new Facesmessage("Login error","MSG ERROR"); } }
– slyfer
puts your answer as a response and you mark as a response after you mark as the solution
– Tmilitino