[英]Can't I access session scope variable using @ManagedProperty?
我知道我可以像这样放置/获取会话范围变量。
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put(SESSION_KEY_SOME, some);
那我不能访问这样的值吗?
@ManagedBean
@SessionScoped
public class SomeOtherBean {
@ManagedProperty("#{sessionScope.some}")
private Some some;
}
该值为null
。
@ManagedProperty
的创建/实例化过程中运行@ManagedBean
。
因此,在第一次设置#{sessionScope.some}
之前创建@ManagedBean
时,它在@ManagedBean
仍将为null
。 仅在首次设置#{sessionScope.some}
之后创建@ManagedBean
时,它才起作用。
基本上有三种方法可以实现所需的行为。
用externalContext.getSessionMap().get("some")
替换private Some some
。
@ManagedBean @SessionScoped public class SomeOtherBean { public void someMethod() { Some some = (Some) FacesContext.getCurrentInstance() .getExternalContext().getSessionMap().get("some"); // ... } }
将@SessionScoped
替换为@RequestScoped
。
@ManagedBean @RequestScoped public class SomeOtherBean { @ManagedProperty("#{sessionScope.some}") private Some some; // ... }
通过直接将其设置为bean属性来替换externalContext.getSessionMap().put("some", some)
。
@ManagedBean public class SomeBean { @ManagedProperty("#{someOtherBean}") private SomeOtherBean someOtherBean; public void someMethod() { // ... someOtherBean.setSome(some); } // ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.