How to make a Login and password screen using JSF

Asked

Viewed 607 times

0

I made a login screen, on this screen the user can register through a registration button, made the registration he can access the system stating password and CPF.

My system will be for environmental complaints, and will have a tax that will receive all complaints made by users.

My question is, how will I make so that when the user logs in, he has access to the user screen, and when the inspector logs in to the tax screen?

Tela Login Bean:

package Bean;

import DAO.UsuarioDAO;
import Domain.Usuario;
import Util.MensagesUtil;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;



/**
 *
 * @author alexandre
 */

@ManagedBean
@RequestScoped
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Usuario usuario = new Usuario();
    private UsuarioDAO usuarioDao;
    private ArrayList<Usuario> itens;



    public String login() throws SQLException {
        usuarioDao = new UsuarioDAO();
         usuario = usuarioDao.obterUsuario(usuario);

         if (usuario != null) {
            return "/telaOpcaoUsuario.xhtml";
        }
         else {
             MensagesUtil.adicionarMensagemErro("Usuario e Senha invalidos !");
             return "/login.xhtml";
         }
    }

    public void Cadastrar() {
     try {
            usuarioDao = new UsuarioDAO();
            usuarioDao.salvar(usuario);

            setItens(usuarioDao.listar());
            MensagesUtil.adicionarMensagemSucesso("Usuario Salvo Com Sucesso");

        } catch (SQLException ex) {
            ex.printStackTrace();
            MensagesUtil.adicionarMensagemErro(ex.getMessage());
        }
    }

    public void Excluir() {
        try {
            usuarioDao = new UsuarioDAO();
            usuarioDao.excluir(usuario);

            setItens(usuarioDao.listar());
            MensagesUtil.adicionarMensagemSucesso("Usuario Removido com Sucesso");

        } catch (SQLException ex) {
            ex.printStackTrace();
            MensagesUtil.adicionarMensagemErro(ex.getMessage());
        }
    }

    public void Editar() {
        try {
            usuarioDao = new UsuarioDAO();
            usuarioDao.editar(usuario);

            setItens(usuarioDao.listar());
            MensagesUtil.adicionarMensagemSucesso("Usuario Editado Com Sucesso");

        } catch (SQLException ex) {
            ex.printStackTrace();;
            MensagesUtil.adicionarMensagemErro(ex.getMessage());

        }
    }


    public UsuarioDAO getUsuarioDao() {
        return usuarioDao;
    }

    public void setUsuarioDao(UsuarioDAO usuarioDao) {
        this.usuarioDao = usuarioDao;
    }

    public ArrayList<Usuario> getItens() {
        return itens;
    }

    public void setItens(ArrayList<Usuario> itens) {
        this.itens = itens;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }
}

Screen LOGIN XHTML:

<?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">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                template="/WEB-INF/template/layout.xhtml">

 <ui:define name="menu">

        <h:outputText value="Bem Vindo ao Sistema DMVA"/>
 </ui:define>

    <ui:define name="metadata"/> 

    <ui:define name="conteudo">
    <h:body>

    <p:messages autoUpdate="true" globalOnly="true" />  
        <h:form id="frm-login">
           <div align="center" >     
          <p:panelGrid  columns="1" columnClasses="ui-grid-col-12" >  
              <p:toolbar >
                <f:facet name="left" >
                    <h:outputText value="Faça Seu Login"  /> 
                </f:facet>                
              </p:toolbar> 

     <!-- PainelGroup Serve para unir os elementos em um coluna -->   
                <h:panelGroup> 
                    <p:outputLabel for="cpf" value="CPF" style="margin-left:17px; font-weight:bold"/>
                    <p:inputMask id="cpf" value="#{loginBean.usuario.cpf}" mask="999.999.999-99" />

                </h:panelGroup>

                <h:panelGroup>
                     <p:outputLabel for="senha" value="Senha" style="font-weight:bold" />
                    <p:password id="senha" maxlength="5" size="20" value="#{loginBean.usuario.senha}"/>
                </h:panelGroup>
                <p:commandButton value="Entrar" action="#{loginBean.login}" update="@form"/>
                <p:commandLink value="Cadastrar" type="button" onclick="PF('dlg3').show();" />      
         </p:panelGrid>
               </div>  
        </h:form>

    <p:dialog header="Cadastre Novo Usuário!!! " widgetVar="dlg3" showEffect="explode" hideEffect="bounce" height="100%" >
        <h:form id="frm-cadastro">        
    <h:panelGrid columns="2" >
        <h:outputLabel for="nome" value="Nome:" style="font-weight:bold"/>
        <p:inputText id="nome" value="#{loginBean.usuario.nome}"/>

        <h:outputLabel for="Endereco" value="Endereço" style="font-weight:bold"/>
         <p:inputText id="Endereco" value="#{loginBean.usuario.endereco}" />

        <h:outputLabel for="cpf" value="CPF:" style="font-weight:bold"/>
        <p:inputMask id="cpf" value="#{loginBean.usuario.cpf}" mask="999.999.999-99" />

        <h:outputLabel for="telefone" value="Telefone:" style="font-weight:bold"/>
         <p:inputMask id="telefone" value="#{loginBean.usuario.telefone}" mask="(99)-9999-9999" />

        <h:outputLabel for="senha" value="Senha" style="font-weight:bold"/>
         <p:password id="Senha" value="#{loginBean.usuario.senha}" />


    </h:panelGrid>
         <p:commandButton value="Salvar" action="#{loginBean.Cadastrar}" oncomplete="PF('dlg3').hide();" update="@form" />
    </h:form>
    </p:dialog>
  </h:body> 


     </ui:define>
</ui:composition>
  • Let me understand the use case. Depending on the type of user the post-login redirection performed is different, is that it? The login screen is the same, but depending on the user, a different screen will appear?

  • Can you do that? because in the system has the tax ad cadastra, and has the user(public), hence the user register as user, I do not know if I was clear

1 answer

0

You will need to identify the profile users in some way, once you are with the defined profiles, just change the targeting page.

if (usuario != null) {
    if (usuario.getPerfil() == 1) {
        return "/telaFISCAL.xhtml";
    } else if (usuario.getPerfil == 2) {
        return "/telaNORMAL.xhtml";
    } else {
        return "/telaERRO.xhtml"
    }
}

If you don’t already have any profile type, you may need to change the registration system. Or just add the column in the table and go manually changing all users in the database.

I put the profiles as 1 and 2, because generally one makes table relationships, in case the table of users would not be written "fiscal" but would have the profile code ID, relating the table of users with the table of profiles, but you could also have the fixed profile as direct text in the users table, so the if would be more or less if ("FISCAL".equals(usuario.getPerfil())) {}

  • Thank you very much, that profile I make a class for it in the Bean, or in the save method?

  • dude I’ll leave you my email I can send you my example running [email protected]

Browser other questions tagged

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