当执行 f:ajax 时,JSF2 preRenderComponent 总是被调用
我有一个由 NewsBean.java 支持的 JSF 页面,其中有
作为 bean 初始值设定项。
页面底部有一个用于发送评论的按钮,其中有:
并被 包围
。单击按钮时,始终会调用 NewsBean.init()
。
我的 bean 范围是视图。这是一个有效的行为(始终调用 init())吗?如何防止总是调用 init()
?
I have an JSF page backed by NewsBean.java which has <f:event type="preRenderComponent" listener="#{newsBean.init}" />
as bean initializer.
There is a button at the bottom of the page for sending comments which has:<f:ajax event="click" execute="@form" render="@form" listener="#{newsBean.sendComment}" />
and is surrounded by an <h:form>
. When button is clicked, NewsBean.init()
is always called.
My bean scope is view. Is this a valid behavior (calling always init())? How can I prevent always calling init()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
preRender
侦听器始终在预渲染事件上调用,无论它是初始请求还是回发请求。每个请求都有一个渲染响应阶段,无论它是普通请求还是 ajax 请求。所以这种行为是符合规范的。您需要在侦听器方法中检查自己是否是回发请求,方法是检查FacesContext#isPostback()
。
(其中Xxx
可以是View
或Component
)顺便说一句,本质上是一种“解决方法”,用于满足在初始请求处理视图参数后能够调用 bean 操作方法的功能要求。在即将推出的 JSF 2.2 中,将引入新的
标记,该标记应该完全按照预期完成工作:该标记支持
onPostback
属性,该属性已默认为false
:JSF 2.2 将于 2012 年第一季度发布。JSF 2.2 的快照版本目前已经可用。
A
preRender
listener is always invoked on pre render event, regardless of if it's an initial request or a postback request. Every single request has a render response phase, regardless of if it's a normal request or ajax request. So this behaviour is by specification. You need to check yourself in the listener method if it's a postback request or not by checkingFacesContext#isPostback()
.The
<f:event type="preRenderXxx">
(whereXxx
can beView
orComponent
) is by the way in essence a "workaround approach" for the functional requirement of being able to invoke a bean action method after the view parameters are been processed on the initial request. In the upcoming JSF 2.2 a new<f:viewAction>
tag will be introduced which should do exactly the job as intented:This tag supports an
onPostback
attribute which already defaults tofalse
:JSF 2.2 will be released the first quart of 2012. Snapshot releases of JSF 2.2 are currently already available.
我猜你的
标签放在
标签内。因此,当您单击 ajax 按钮时,它会重新渲染整个
组件,从而导致 preRenderComponent 事件再次触发。我认为你应该使用的是 PreRenderViewEvent。
I guess your
<f:event>
tag is put inside the<h:form>
tag. Hence, when you click the ajax button, it re-render the whole<h:form>
component which results in the preRenderComponent event being triggered again.I think what you should use is PreRenderViewEvent.