How to access the resource of one managed bean in another managed bean

I have a managed bean (SessionScope, as shown below)

@ManagedBean(name="login") @SessionScoped public class Login implements Serializable { private String userSession; public Login(){ } } 

In this managed folder, somewhere in the login function, I store the email as a session.

I have another managed bean called ChangePassword (ViewScoped). I need to access the email value that is stored in userSession.

The reason for this is that I need to know the current userSession (email) before I can execute the password change function. (You must change the password for this particular letter)

How can I do it? New to JSF, appreciate any help!

+7
source share
4 answers

Just enter one bean as a managed property of another bean.

 @ManagedBean @ViewScoped public class ChangePassword { @ManagedProperty("#{login}") private Login login; // +setter (no getter!) public void submit() { // ... (the login bean is available here) } // ... } 

See also:

+16
source

In JSF2, I usually use this method:

 public static Object getSessionObject(String objName) { FacesContext ctx = FacesContext.getCurrentInstance(); ExternalContext extCtx = ctx.getExternalContext(); Map<String, Object> sessionMap = extCtx.getSessionMap(); return sessionMap.get(objName); } 

The input parameter is the name of your bean.

+2
source

if your session bean coverage is as follows:

 @ManagedBean(name="login") @SessionScoped public class Login implements Serializable { private String userSession; public Login(){ } } 

you can access the values ​​of this bean, for example:

 @ManagedBean(name="changePassword") @ViewScoped public class ChangePassword implements Serializable { @ManagedProperty(value="#{login.userSession}") private String userSession; public ChangePassword (){ } } 
0
source
 public static Object getSessionObj(String id) { return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(id); } public static void setSessionObj(String id,Object obj){ FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(id, obj); } 

Add them to a managed bean:

0
source

All Articles