1. java
  2. android
  3. c#
  4. .net
  5. javascript
  6. php
  7. jquery
  8. html
  9. sql

Login Hibernate não redireciona de acordo com o faces-config.xml (login hibernate primefaces)

Bom dia, sou novato por aqui e estou com um problema, o login é feito com sucesso, porém não é feito o redirecionamento para a pagina descrita no faces-config.xml. Poderiam me dar uma luz :) Agradeço desde já




- faces-config.xml
```<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
 <managed-bean>
  <managed-bean-name>LoginBean</managed-bean-name>
  <managed-bean-class>edu.gaed.bean.LoginBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>UFBean</managed-bean-name>
  <managed-bean-class>edu.gaed.bean.UFBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>MunicipioBean</managed-bean-name>
  <managed-bean-class>edu.gaed.bean.MunicipioBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>BairroBean</managed-bean-name>
  <managed-bean-class>edu.gaed.bean.BairroBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>LogradouroBean</managed-bean-name>
  <managed-bean-class>edu.gaed.bean.LogradouroBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/login.xhtml</from-view-id>
  <navigation-case>
   <from-outcome>home</from-outcome>
   <to-view-id>/home.xhtml</to-view-id>
  </navigation-case>
  <navigation-case>
   <from-outcome>login</from-outcome>
   <to-view-id>/login.xhtml</to-view-id>
   <redirect/>
  </navigation-case>
 </navigation-rule>
 <application/>
</faces-config>
  • LoginDao

package edu.gaed.dao;


import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import edu.gaed.util.HibernateUtil;
import edu.gaed.vo.Usuario;



public class LoginDao{      

    private static Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    //Login
    public static boolean login(String login, String senha) {

        Transaction transaction = null;
        try {
          session = HibernateUtil.getSession();
          transaction  = session.beginTransaction();
          Criteria criteria = session.createCriteria(Usuario.class);
             //na tabela usuário deve constar os campos login e senha.
            criteria.add(Restrictions.eq("login", login));
            criteria.add(Restrictions.eq("senha", senha));
           System.out.println((Usuario) criteria.uniqueResult()!=null) ;
          //transaction.commit();
          return (Usuario) criteria.uniqueResult()!=null;
        }
        catch (RuntimeException e) {
            transaction.rollback();
            throw e;
        }   

        /*
        session.beginTransaction();
            Criteria criteria = session.createCriteria(Usuario.class);
             //na tabela usuário deve constar os campos login e senha.
            criteria.add(Restrictions.eq("login", login));
            criteria.add(Restrictions.eq("senha", senha));
            //session.getTransaction().commit();

            return (Usuario) criteria.uniqueResult()!=null;*/        
     }
}
  • LoginBean


package edu.gaed.bean;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import edu.gaed.dao.LoginDao;

@ManagedBean(name="LoginBean")
@SessionScoped
public class LoginBean {

    public LoginBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    public LoginBean(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    private String username;

    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String loginProject() {
        boolean result = LoginDao.login(username, password);
        if (result) {
            System.out.println("home");
            return "home";
        } else {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Login invalido!",
                    "Tente novamente!"));
            return "login";
        }
    }
}
  • login.xhtml


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>login</title>
    </h:head>
    <h:body>
        <h:form id="loginForm">  
            <p:growl id="msg" showDetail="true" life="3000" />
            <p:panel header="Login" style="width: 360px;">
                <h:panelGrid id="loginPanel" columns="2">
                    <h:outputText value="Username" />

                <p:inputText id="username" value="#{LoginBean.username}" ></p:inputText>

                    <p:spacer></p:spacer>

                    <p:message for="username" ></p:message>

                    <h:outputText value="Password" />

                    <p:password id="password" value="#{LoginBean.password}"  feedback="false"></p:password>

                    <p:spacer></p:spacer>

                    <p:message for="password"></p:message>

                    <p:spacer></p:spacer>

                    <h:commandButton actionListener="#{LoginBean.loginProject}" value="Login" update="loginForm" ajax="true"></h:commandButton>

                </h:panelGrid>

            </p:panel>

        </h:form>

    </h:body> 

</html>
  1. Você vai ver essas setas em qualquer página de pergunta. Com elas, você pode dizer se uma pergunta ou uma resposta foram relevantes ou não.
  2. Edite sua pergunta ou resposta caso queira alterar ou adicionar detalhes.
  3. Caso haja alguma dúvida sobre a pergunta, adicione um comentário. O espaço de respostas deve ser utilizado apenas para responder a pergunta.
  4. Se o autor da pergunta marcar uma resposta como solucionada, esta marca aparecerá.
  5. Clique aqui para mais detalhes sobre o funcionamento do GUJ!

1 resposta

Não é a resposta que estava procurando? Procure outras perguntas com as tags login hibernate primefaces ou faça a sua própria pergunta.