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:
- if I assign a object a sessionMap key, and I try to retrieve a string attribute from the object, the value is not displayed.
- if I assign the value of a variable string to the key, the value is not displayed.
- 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
}