@PostConstruct方法会被调用,即使ManagedBean已经被实例化(例如在ajax调用上)[duplicate]

时间:2022-10-08 12:53:44

This question already has an answer here:

这个问题已经有了答案:

I have a @ViewScope ManagedBean and a @PostConstruct initialisation method. This method is called when a new instance is created, but also on every ajax call. Why is this so?

我有一个@ViewScope ManagedBean和一个@PostConstruct初始化方法。在创建新实例时调用此方法,在每次ajax调用时也调用此方法。为什么会这样呢?

On an AJAX-call the init-Method is called and executed, but no changes are visible. For example if I change a property in the init-Method, this is only visible on instatiation and not for AJAX-calls. For AJAX-calls the value change is not persistent in the @ViewScoped Bean.

在ajax调用中,将调用并执行init方法,但不可见任何更改。例如,如果我在init方法中更改一个属性,这只在instatiation中可见,而不是在ajax调用中可见。对于ajax调用,值更改在@ViewScoped Bean中不是持久的。

Can anyone tell why this is so? How can I change this?

有人知道为什么会这样吗?我如何改变这个?

1 个解决方案

#1


11  

This is not normal behavior. This will happen if you bind tag handler attributes or the binding attribute of JSF components to a property of a view scoped bean while partial state saving is turned on. This is known as issue 1492 which is fixed in (the upcoming) Mojarra 2.2.

这不是正常的行为。当打开部分状态保存时,如果将标记处理程序属性或JSF组件的绑定属性绑定到视图作用域bean的属性,就会发生这种情况。这被称为第1492期,将在(即将到来的)Mojarra 2.2中解决。

In general, you can recognize tag handlers by the lack of the rendered attribute. E.g. <c:if>, <f:validator>, <ui:include>, etc. If you bind an attribute of such a tag handler to a property of the view scoped bean like follows

通常,您可以通过缺少呈现的属性来识别标记处理程序。例: 等。如果您将这样一个标记处理程序的属性绑定到视图作用域bean的属性,如下所示 包括> 如果>

<c:if test="#{viewScopedBean.something}"></c:if>
<h:inputText><f:validator binding="#{viewScopedBean.validate}" /></h:inputText>
<ui:include src="#{viewScopedBean.includePage}" />

then the view scoped bean will be recreated everytime the view is to be restored from a partially saved state. This is a chicken-egg issue with the view scope, because in order to get the right view scoped bean, it has to be extracted from the restored view.

然后,每次视图从部分保存的状态中恢复时,都会重新创建视图作用域bean。这是视图范围内的鸡-蛋问题,因为为了获得正确的视图范围bean,必须从恢复的视图中提取它。

This will also happen if you reference a property of a view scoped bean in the binding attribute of a JSF component.

如果在JSF组件的绑定属性中引用视图作用域bean的属性,也会发生这种情况。

<h:someComponent binding="#{viewScopedBean.someComponent}" />

See also:

#1


11  

This is not normal behavior. This will happen if you bind tag handler attributes or the binding attribute of JSF components to a property of a view scoped bean while partial state saving is turned on. This is known as issue 1492 which is fixed in (the upcoming) Mojarra 2.2.

这不是正常的行为。当打开部分状态保存时,如果将标记处理程序属性或JSF组件的绑定属性绑定到视图作用域bean的属性,就会发生这种情况。这被称为第1492期,将在(即将到来的)Mojarra 2.2中解决。

In general, you can recognize tag handlers by the lack of the rendered attribute. E.g. <c:if>, <f:validator>, <ui:include>, etc. If you bind an attribute of such a tag handler to a property of the view scoped bean like follows

通常,您可以通过缺少呈现的属性来识别标记处理程序。例: 等。如果您将这样一个标记处理程序的属性绑定到视图作用域bean的属性,如下所示 包括> 如果>

<c:if test="#{viewScopedBean.something}"></c:if>
<h:inputText><f:validator binding="#{viewScopedBean.validate}" /></h:inputText>
<ui:include src="#{viewScopedBean.includePage}" />

then the view scoped bean will be recreated everytime the view is to be restored from a partially saved state. This is a chicken-egg issue with the view scope, because in order to get the right view scoped bean, it has to be extracted from the restored view.

然后,每次视图从部分保存的状态中恢复时,都会重新创建视图作用域bean。这是视图范围内的鸡-蛋问题,因为为了获得正确的视图范围bean,必须从恢复的视图中提取它。

This will also happen if you reference a property of a view scoped bean in the binding attribute of a JSF component.

如果在JSF组件的绑定属性中引用视图作用域bean的属性,也会发生这种情况。

<h:someComponent binding="#{viewScopedBean.someComponent}" />

See also: