使用此bean
处理模型@ManagedBean(name="login")
@SessionScoped
public class LoginBean {
private String login ;
private String pwd ;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
虚拟登录后,我需要使用我的@managedProperty来设置我的下一个bean:
//Only for proving concepts
@ManagedProperty(value="#{login}")
private LoginBean loginBean;
public LoginBean getLoginBean() {
return loginBean;
}
public void setLoginBean(LoginBean loginBean) {
this.loginBean = loginBean;
}
我需要设置nextPage的bean调用它:
@PostConstruct
public void init() {
if (loginBean.getLogin()) {
...
}
}
但是这个bean保持为null:
Caused by: java.lang.NullPointerException
at fr.maf.vega.web.control.RechercheDossierBean.init(RechercheDossierBean.java:441)
感谢您的建议...
注意: 使用以下方法获取@SessionScoped loginBean效果很好(但它似乎不属于最佳实践):
public static Object getManagedBean(final String beanName) {
FacesContext fc = FacesContext.getCurrentInstance();
Object bean;
try {
ELContext elContext = fc.getELContext();
bean = elContext.getELResolver()
.getValue(elContext, null, beanName);
} catch (RuntimeException e) {
throw new FacesException(e.getMessage(), e);
}
if (bean == null) {
throw new FacesException(
"Managed bean with name '"
+ beanName
+ "' was not found. Check your faces-config.xml or @ManagedBean annotation.");
}
return bean;
}