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:
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
}