package br.com.caelum.livraria.dao;
import java.io.Serializable;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import br.com.caelum.livraria.modelo.Usuario;
@SuppressWarnings("serial")
public class UsuarioDao implements Serializable {
@Inject
EntityManager manager;
public boolean existe(Usuario usuario) {
System.out.println("usuario:" + usuario.getEmail());
System.out.println("senha:" + usuario.getSenha());
TypedQuery<Usuario> query = manager.createQuery("select u from Usuario u where u.email = :pEmail and u.senha = :pSenha",Usuario.class);
query.setParameter("pEmail", usuario.getEmail());
query.setParameter("pSenha", usuario.getSenha());
try {
Usuario resultado = query.getSingleResult();
} catch (NoResultException ex){
return false;
}
return true;
}
}
package br.com.caelum.livraria.bean;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import br.com.caelum.livraria.dao.UsuarioDao;
import br.com.caelum.livraria.modelo.Usuario;
@Named
@ViewScoped
@SuppressWarnings("serial")
public class LoginBean implements Serializable {
@Inject
UsuarioDao usuarioDao;
@Inject
FacesContext context;
private Usuario usuario = new Usuario();
public Usuario getUsuario() {
return usuario;
}
public String efetuarLogin() {
System.out.println("Efetuando login do usuario" + this.usuario.getEmail());
boolean existe = usuarioDao.existe(this.usuario);
if (existe) {
context.getExternalContext().getSessionMap().put("usuarioLogado", this.usuario);
return "livro?faces-redirect=true";
}
context.addMessage(null, new FacesMessage("Usuario nao encontrado"));
return "login";
}
public String deslogar() {
context.getExternalContext().getSessionMap().remove("usuarioLogado");
return "login?faces-redirect=true";
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="_template.xhtml">
<ui:define name="titulo">
<p:outputPanel>Login</p:outputPanel>
</ui:define>
<ui:define name="conteudo">
<p:messages globalOnly="true"/>
<h:form id="login">
<p:fieldset legend="Login">
<h:panelGrid columns="3">
<p:outputLabel value="Email:" for="email" />
<p:inputText id="email" value="#{loginBean.usuario.email}" required="true">
<f:passThroughAttribute name="type" value="email" />
</p:inputText>
<p:message for="email" id="messageEmail" />
<p:outputLabel value="Senha:" for="senha" />
<p:password id="senha" value="#{loginBean.usuario.senha}" feedback="true" required="true" />
<p:message for="senha" id="messageSenha" />
<p:commandButton value="Efetue login" action="#{loginBean.efetuarLogin}" update="@form" process="@form" />
</h:panelGrid>
</p:fieldset>
</h:form>
</ui:define>
</ui:composition>
</html>
PARTE DO LOG:
INFO: Starting ProtocolHandler ["ajp-nio-8009"] Aug 02, 2016 10:38:35 AM org.apache.catalina.startup.Catalina start INFO: Server startup in 15568 ms FASE: RESTORE_VIEW 1 FASE: RESTORE_VIEW 1 /login.xhtml FASE: RENDER_RESPONSE 6 /login.xhtml FASE: APPLY_REQUEST_VALUES 2 FASE: PROCESS_VALIDATIONS 3 FASE: UPDATE_MODEL_VALUES 4 FASE: INVOKE_APPLICATION 5 Efetuando login do usuarionull usuario:null senha:null Aug 02, 2016 10:38:39 AM org.hibernate.annotations.common.Version INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} Aug 02, 2016 10:38:39 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.1.8.Final} Aug 02, 2016 10:38:39 AM org.hibernate.cfg.Environment
Projeto no bitbucket https://bitbucket.org/arogeriop/livraria.git