如何在另一个托管Bean中访问一个托管Bean的属性 [英] How to access property of one managed bean in another managed bean
问题描述
我有一个托管bean(SessionScope如下)
I have a managed bean (SessionScope as follow)
@ManagedBean(name="login")
@SessionScoped
public class Login implements Serializable {
private String userSession;
public Login(){
}
}
在此托管bean中,我在登录功能中的某处将电子邮件存储为会话.
In this managedbean, somewhere in the login function, i store the email as a session.
我还有一个名为ChangePassword(ViewScoped)的托管bean.我需要访问存储在userSession中的电子邮件的值.
I have another managed bean called ChangePassword (ViewScoped). I need to access the value of the email which is stored in the userSession.
这样做的原因是,我需要先找到当前的userSession(email),然后才能完成更改密码功能. (需要更改该特定电子邮件的密码)
The reason of doing so is that i need to find out the current userSession(email) before i can complete the change password function. (Need change password for that specific email)
我该怎么做? JSF的新手,感谢您的帮助!
How do i do so? New to JSF, appreciate any help!
推荐答案
只需将一个bean作为另一个bean的托管属性进行注入.
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)
}
// ...
}
另请参见:
- JSF 2.0中的通信-在彼此
- Communication in JSF 2.0 - Injecting managed beans in each other
See also:
这篇关于如何在另一个托管Bean中访问一个托管Bean的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!