Usually during login proses, we validate the user credential, fetch the user details from database and store it in a session for the future use. Normally HttpSession is used to achieve this goal. In Seam we do this injection by using @In and @Out annotation, in JSF 2, we use @ManagedProperty.
Consider we have a login bean with request scope and a user session bean with session scope. So it would be like this :
1.user session bean
@ManagedBean(name="userSession")
@SessionScoped
public class UserSession {
private String username;
//getter and setter for username
}
2.login bean
@ManagedBean(name="loginActionBean")
@RequestScoped
public class loginActionBean {
@ManagedProperty(value = "#{userSession}")
private UserSession userSession ;
//getter and setter for userSession
private String username;
//getter and setter for username
public String authenticate() {
userSession.setUsername(this.username);
return "nextpage.jsf";
}
}
then we can get the user session value in other managed beans or our view(xhtml) by calling something like userSession.getUsername or #{userSession.username} . Again, @ManagedProperty(value = "#{userSession}") with its getter and setter must be set in the managed bean where we want to use the session.
Alternatively, we can also use FacesContext to set and get the session.