JSF login with roles, nullpointerexception

UI Components for JSF
Post Reply
Laval
Posts: 1
Joined: 23 Apr 2019, 04:24

12 Oct 2019, 08:24

Hi, I need help, I was making a login with users roles to activate or deactivate the buttons as required, and it worked.
My problem is that when I click in one option to go to other page, Im receiving a nullpointerexception, and I really dont not why.

Here is my code:

loginBean.java

Code: Select all

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pic.bean;

import java.util.List;
import java.util.Objects;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import pic.dao.loginDAO;
import pic.modelo.Usuario;

/**
 *
 * @author Miki
 */
@ManagedBean
@SessionScoped
public class loginBean {
    private Usuario usuario;
    /**
     * Creates a new instance of loginBean
     */
    public loginBean(){
        this.usuario = new Usuario();
    }

    public Usuario getUsuario(){
        return usuario;
    }

    public void setUsuario(Usuario usuario){
        this.usuario = usuario;
    }
    
    public String inciarSession(){
        loginDAO loDao = new loginDAO();
        usuario = loDao.getUsuario(usuario.getUsuario(), usuario.getContrasena());
                
        if(usuario.getIdUsuario() != 0){
            List<Object[]> permisos = loDao.getPermisos(usuario.getIdUsuario());

            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().getSessionMap().put("usuario", usuario);
            context.getExternalContext().getSessionMap().put("permisos", permisos);
            
            return "menu";
        }else{
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Error", "Usuario no indentificado, revisar contraseña o nombre de Usuario"));
        }
        return null;
    }
    
    public String cerrarSession(){
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "/login";
    }
    
    
    public boolean permisoM(List<Object[]> permisos, boolean maestro, String pantalla){
        
        if(!maestro){
            Byte pant = Byte.parseByte(pantalla);
            for(Object[] permiso : permisos){
                if(Objects.equals(pant, (Byte) permiso[0]))
                    return false;
            }
            return true;
            
        }else{
            return false;
        }
    }
    
    public boolean permisoSubM(List<Object[]> permisos, boolean maestro, String pantalla){
        FacesContext.getCurrentInstance();
        if(!maestro){
            Byte pant = Byte.parseByte(pantalla);
            for(Object[] permiso : permisos){
                if(Objects.equals(pant, (Byte) permiso[0]))
                    return false;
            }
            return true;
        }else{
            return false;
        }
    }
}
plantillaMenu.xhtml

Code: Select all

<?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:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <h:outputStylesheet name="./css/default.css"/>
        <h:outputStylesheet name="./css/cssLayout.css"/>   
        
        <title>Menu</title>
        <link rel="icon" type="image/jpg" href="resources/imagenes/forIcon.ico"/>
        
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0"></meta>
    
    </h:head>

    <h:body>
        <div id="top">
            <ui:insert name="top"></ui:insert>
            <div id="menuBar">
                <h:form>
                    <p:menubar autoDisplay="false" style="background: #E6F8F7">
                        <p:submenu label="Maestros" icon="ui-icon-document" styleClass="colorBotonMenu" >
                            <p:menuitem value="Cliente" outcome="/maestros/cliente" styleClass="colorBotonItem" disabled="#{loginBean.permisoM(sessionScope.permisos, sessionScope.usuario.maestro, '1')}"/>
                            <p:menuitem value="Usuario" outcome="/maestros/usuario" styleClass="colorBotonItem" disabled="#{loginBean.permisoM(sessionScope.permisos, sessionScope.usuario.maestro, '2')}"/>
                        </p:submenu>
                        <p:menuitem value="Cerrar Session" outcome="#{loginBean.cerrarSession()}" styleClass="colorBotonItem"/>
                    </p:menubar>
                </h:form>                
            </div>
        </div>

        <div id="content" class="center_content">
            <ui:insert name="content"></ui:insert>
        </div>

        <center>    
            <p:graphicImage cache="false" value="/resources/imagenes/pc.jpg" alt="Logotipo de la empresa" width="36%" height="auto"/>
        </center>

        <div id="bottom" align="right" style="font-size: 15px; font-weight: bold; font-style: italic;
             color: #29385a" >
            <ui:insert name="bottom"  ></ui:insert>
            
        </div>
    </h:body>

</html>
login.xhtml

Code: Select all

<?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:p="http://primefaces.org/ui">
    
    <h:head>
        <title>Login</title>
    </h:head>
    <h:body>
        <h:form id="form-login">
            <p:growl id="mensaje" showDetail="true" sticky="true" autoUpdate="true"/>
            <h:outputLabel for="username" value="Usuario" />
            <h:inputText id="username" value="#{loginBean.usuario.usuario}" required="true" requiredMessage="Usuario Requerido" />
            <br/>
            <h:outputLabel for="password" value="Contraseña" />
            <h:inputSecret id="password" value="#{loginBean.usuario.contrasena}" required="true" requiredMessage="Contraseña Requerida" />
            <br/>
            <h:commandButton value="Acceder" styleClass="btn btn-large" action="#{loginBean.inciarSession()}"/>
        </h:form>
    </h:body>
</html>

menu.xhtml

Code: Select all

<?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:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

    <body>
        <ui:composition template="./plantillas/plantillaMenu.xhtml">
            <ui:define name="content">                
            </ui:define>
        </ui:composition>
    </body>
</html>

So for example I use an user that have the permissions to use the option "cliente", so this option will be enable and the other option ("usuario") will be unable, but when I click "cliente" I recieve the null pointer...

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

13 Oct 2019, 19:05

1: Your 'login.xhtml' contains no relevant primefaces components. 2: neither does your bean. 3: You don't state anywhere where the NPE occurs, 4: You don't specify what you investigated regarding the cause. 5:...

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 22 guests