为每个请求创建的jsf 2会话bean [英] jsf 2 Session bean created for every request
问题描述
你好
我有2个托管bean,一个View范围,另一个Session范围. View作用域bean定义为
I have 2 Managed beans, one View scoped, the other Session scoped. The View scoped bean is defined as
@ManagedBean
@ViewScoped
public class InvoiceController implements Serializable {
private static final long serialVersionUID = 1L;
@ManagedProperty(value="#{invoiceService}")
private InvoiceService invoiceService;
会话作用域为
@ManagedBean
@SessionScoped
public class InvoiceService implements Serializable{
我正在使用会话范围的bean来保存用于决定是否应呈现面板的标志,当我通过调试运行此标志时,我发现每次我在sesison bean上调用该方法时,它都是一个新实例,因此在两次请求之间不保留我的标志的值.
I am using the session scoped bean to hold a flag used to decide if a panel should be rendered, when I run this through the debug I find that every time I call the method on the sesison bean, it is a new instance of the bean and therefore does not retain the value of my flag between requests.
我在做什么错了?
推荐答案
如果您是从 javax.faces.bean
包.前者仅适用于CDI @Named
bean,而后者适用于JSF @ManagedBean
仅限豆子.
That can happen if you have imported @SessionScoped
from the javax.enterprise.context
package instead of from the javax.faces.bean
package. The former works on CDI @Named
beans only, while the latter works on JSF @ManagedBean
beans only.
没有任何有效范围的JSF @ManagedBean
将默认为 @ManagedProperty
.这就解释了您所看到的症状.
A JSF @ManagedBean
without any valid scope would default to @NoneScoped
which means that it's newly constructed on every single EL expression referencing the bean, such as the @ManagedProperty
. This explains the symptoms you're seeing.
这篇关于为每个请求创建的jsf 2会话bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!