即使 ManagedBean 已经被实例化(例如在 AJAX 调用上),@PostConstruct 方法也会被调用 [英] @PostConstruct method is called even if the ManagedBean has already been instantiated (e.g. on AJAX-calls)

查看:31
本文介绍了即使 ManagedBean 已经被实例化(例如在 AJAX 调用上),@PostConstruct 方法也会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在 AJAX 调用中,init-Method 被调用并执行,但没有任何变化可见.例如,如果我更改了 init-Method 中的一个属性,则这仅在实例化时可见,而对 AJAX 调用不可见.对于 AJAX 调用,@ViewScoped Bean 中的值更改不是持久的.

谁能告诉我为什么会这样?我该如何更改?

解决方案

这不是正常行为.如果您将标记处理程序属性或 JSF 组件的 binding 属性绑定到视图范围 bean 的属性,同时部分状态保存处于打开状态,则会发生这种情况.这称为 issue 1492,已在(即将推出的)Mojarra 2.2 中修复.>

通常,您可以通过缺少 rendered 属性来识别标记处理程序.例如. 等.像下面这样的视图作用域 bean 属性的标记处理程序

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

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

如果您在 JSF 组件的 binding 属性中引用视图范围 bean 的属性,也会发生这种情况.

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

另见:

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?

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.

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

解决方案

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.

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

<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.

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

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

See also:

这篇关于即使 ManagedBean 已经被实例化(例如在 AJAX 调用上),@PostConstruct 方法也会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆