How to display the String value retrieved from the JSF session?

1

I'm having a problem trying to display a String value retrieved from the session in xhtml. What I want is to show the value of a String attribute (name attribute) of an object of the class I created (User class), which is stored in the session.

The curious thing is that:

  • If I assign a object to a sessionMap key, and try to retrieve a string attribute from the object, the value is not displayed.
  • If you assign the value of a string variable to the key, the value is not displayed.
  • If I assign a string literal value to the key, then the value is displayed.
  • Example :

    test.xhtml

    At outputs s1 to s3, the name of the class concatenated with the hash code of the object is displayed. At exits s4 to s9, nothing is displayed.
    At output s10, the word test is shown (the literal assigned to the session map in the LoginBean).

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!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">
    
    <h:head>
    
    </h:head>
    <h:body>
        <h:outputText rendered="#{not empty usuarioLogado}"
            value=" 
            s1 #{usuarioLogado}
            s2 #{sessionScope.usuarioLogado}
            s3 #{sessionScope['usuarioLogado']}
            s4 #{usuarioLogado.nome}
            s5 #{sessionScope.usuarioLogado.nome}
            s6 #{sessionScope['usuarioLogado'].nome}
            s7 #{nomeUsuarioLogado}
            s8 #{sessionScope.nomeUsuarioLogado}            
            s9 #{sessionScope['usuarioLogado'].nome}
            s10 #{sessionScope.string_literal} ">
        </h:outputText>
    
    </h:body>
    </html>
    

    LoginBean.java

    @ManagedBean
    @SessionScoped
    public class LoginBean {
    
        private Usuario usuario;
    
        @PostConstruct
        public void init(){
            this.usuario = new Usuario();
        }
    
        public LoginBean() {
    
        }
    
        //getters and setters
    
        public String efetuarLogin(){
    
            EntityManager em = JPAUtil.getEntityManager();
            boolean existeUsuario = new UsuarioDao(em).consultarUsuario(usuario);
            em.close();
    
            FacesContext context = FacesContext.getCurrentInstance();
    
            if (existeUsuario) {
                context.getExternalContext().getSessionMap().put("usuarioLogado", this.usuario);
                context.getExternalContext().getSessionMap().put("nomeUsuarioLogado", this.usuario.getNome());
                context.getExternalContext().getSessionMap().put("string_literal", "teste");
    
                return "index.xhtml?faces-redirect=true";
            }
    
            return null;
        }
    
    }
    

    User.java

    public class Usuario {
    
        private int id;
        private String nome;
        private String login;
        private String senha;
        private String email;
    
        public Usuario() {
            // TODO Auto-generated constructor stub
        }
    
        //getters and setters
    
    }
    
        
    asked by anonymous 21.12.2016 / 05:21

    1 answer

    1

    I noticed the failure. The name attribute was empty. In the login screen, only the login (username) and password of LoginBean.usuario were reported, and these data were used as criteria for searching the database through of the UsuarioDao.consultarUsuario(Usuario) method. But, this method only returned a boolean. The solution was to change the UsuarioDao.consultarUsuario(Usuario) method so that, instead of a boolean, it would return a User (or null) object. This object, yes, fully populated, even with the name attribute.

    LoginBean.java looks like this:

    @ManagedBean
    @SessionScoped
    public class LoginBean {
    
        private Usuario usuario;
    
        @PostConstruct
        public void init(){
            this.usuario = new Usuario();
        }
    
        public LoginBean() {
    
        }
    
        //getters and setters
    
        public String efetuarLogin(){
    
            EntityManager em = JPAUtil.getEntityManager();
            this.usuario = new UsuarioDao(em).consultarUsuario(this.usuario);
            em.close();
    
            FacesContext context = FacesContext.getCurrentInstance();
    
            if (existeUsuario) {
                context.getExternalContext().getSessionMap().put("usuarioLogado", this.usuario);
    
                return "index.xhtml?faces-redirect=true";
            }
    
            this.usuario = new Usuario();
    
            return null;
        }
    
    }
    
        
    22.12.2016 / 16:02