即使 ManagedBean 已经实例化(例如在 AJAX 调用上),也会调用 @PostConstruct 方法
我有一个 @ViewScope ManagedBean 和一个 @PostConstruct 初始化方法。创建新实例时以及每次 ajax 调用时都会调用此方法。为什么会这样呢?
在 AJAX 调用中,会调用并执行 init-Method,但看不到任何更改。例如,如果我更改 init-Method 中的属性,则该属性仅在实例化时可见,而对于 AJAX 调用则不可见。对于 AJAX 调用,值更改不会持久保存在 @ViewScoped Bean 中。
谁能告诉我为什么会这样吗?我怎样才能改变这个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是正常行为。如果在打开部分状态保存的情况下将标记处理程序属性或 JSF 组件的
binding
属性绑定到视图作用域 bean 的属性,就会发生这种情况。这称为问题 1492,已在(即将发布的)Mojarra 2.2 中修复。一般来说,您可以通过缺少
rendered
属性来识别标记处理程序。例如
、
、
等。如果绑定了一个属性将这样的标签处理程序添加到视图作用域 bean 的属性中,如下所示,那么每次要从部分保存的状态恢复视图时,都会重新创建视图作用域 bean。这是视图作用域的先有鸡还是先有蛋的问题,因为为了获取正确视图作用域 bean,必须从恢复的视图中提取它。
如果您在 JSF 组件的
binding
属性中引用视图作用域 bean 的属性,也会发生这种情况。另请参阅:
@ViewScoped
在标签处理程序中失败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 followsthen 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.See also:
@ViewScoped
fails in tag handlers