How to access property of one managed bean in another managed bean

 ̄綄美尐妖づ 提交于 2019-12-03 04:02:48

Just inject the one bean as a managed property of the other 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:

In JSF2, I usually use a method like this:

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.

if your session scoped bean is like this :

@ManagedBean(name="login")
@SessionScoped
public class Login implements Serializable {

   private String userSession;
   public Login(){
   }
}

you can access the values of this bean like :

@ManagedBean(name="changePassword")
@ViewScoped
public class ChangePassword implements Serializable {

   @ManagedProperty(value="#{login.userSession}")
   private String userSession;
   public ChangePassword (){
   }
}
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 in your managed bean :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!