应用程序范围的bean的视图未更新

问题描述:

我在应用程序范围内的bean中有一个变量.用户可以通过方法调用触发此变量的更新.现在的问题是,刷新jsf页面后,用户无法获得此变量的更新视图.如果已经测试了该变量是否正确更新,那么更新方法可以正常工作.是在应用程序范围内的bean中将变量声明为final还是在这里出了什么问题?

I have a variable inside an application scoped bean. A user can trigger an update of this variable through a method call. Now the problem is that the user doesn't get an updated view of this variable after refreshing the jsf page. If have tested if the variable is updated properly and it is, so the method for updating is working correctly. Are variables inside an application scoped bean declared as final or what is the problem here?

如果您使用了错误的注释组合,则可能会发生这种情况.例如

That can happen if you used the wrong combination of annotations. E.g.

import javax.enterprise.context.ApplicationScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean
@ApplicationScoped
public class App {}

在这里,范围注释来自CDI,bean管理注释来自JSF. JSF无法识别CDI范围注释,因此默认为@NoneScoped. IE.在每个EL #{app}评估中都会重新构造bean.这就解释了您所看到的症状.

Here, the scope annotation is from CDI and the bean management annotation is from JSF. JSF doesn't recognize CDI scope annotations and hence defaults to @NoneScoped. I.e. the bean is reconstructed on every single EL #{app} evaluation. This explains the symptoms you're seeing.

您还需要将范围注释也修复为来自JSF.

You'd need to fix the scope annotation to be from JSF as well.

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean
@ApplicationScoped
public class App {}

CDI作用域注释只能与CDI bean管理注释@Named结合使用.

The CDI scope annotations can only be used in combination with the CDI bean management annotation @Named.