How to display the recovered String value of the JSF session?

Asked

Viewed 579 times

1

I am facing a problem when trying to display, in xhtml, a String value recovered from session.
What I want is to show the value of a String attribute (attribute name) of an object of the class I created (class User), which is stored in the session.

The funny thing is:

  1. if I assign a object a sessionMap key, and I try to retrieve a string attribute from the object, the value is not displayed.
  2. if I assign the value of a variable string to the key, the value is not displayed.
  3. if I assign a literal string value to the key, then the value is displayed.

Example:

xhtml test.

On S1 to S3 outputs, the name of the class concatenated with the object’s hash code is displayed.
On S4 to S9 outputs, nothing is displayed.
In S10 output, the word test is displayed (the literal assigned to the Session map in 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;
    }

}

Java user.

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

}

1 answer

1


I understood the flaw. The attribute name was really empty.
On the login screen, only the login (username) and the password of Loginbean.usuario, and these data were used as criteria for searching in the database, using the method UsuarioDao.consultarUsuario(Usuario). But, this method returned only a Boolean.
The solution was to change the method UsuarioDao.consultarUsuario(Usuario)so that instead of a Boolean, it would return a User object (or null). This object, yes, fully populated, including with the attribute name.

The Loginbean.java was 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;
    }

}

Browser other questions tagged

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