[英]JSF2 preRenderComponent is called always when f:ajax is executed
I have an JSF page backed by NewsBean.java which has <f:event type="preRenderComponent" listener="#{newsBean.init}" />
as bean initializer. 我有一个由NewsBean.java支持的JSF页面,其中
<f:event type="preRenderComponent" listener="#{newsBean.init}" />
作为bean初始化程序。
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>
. 页面底部有一个按钮,用于发送注释:
<f:ajax event="click" execute="@form" render="@form" listener="#{newsBean.sendComment}" />
和被<h:form>
包围。 When button is clicked, NewsBean.init()
is always called. 单击按钮时,始终会调用
NewsBean.init()
。
My bean scope is view. 我的bean范围是视图。 Is this a valid behavior (calling always init())?
这是一个有效的行为(总是调用init())? How can I prevent always calling
init()
? 如何防止始终调用
init()
?
A preRender
listener is always invoked on pre render event, regardless of if it's an initial request or a postback request. 无论是初始请求还是回发请求,都会在预呈现事件上调用
preRender
侦听器。 Every single request has a render response phase, regardless of if it's a normal request or ajax request. 每个请求都有一个呈现响应阶段,无论它是普通请求还是ajax请求。 So this behaviour is by specification.
所以这种行为是通过规范。 You need to check yourself in the listener method if it's a postback request or not by checking
FacesContext#isPostback()
. 如果是回发请求,则需要通过检查
FacesContext#isPostback()
来检查自己的侦听器方法。
public void sendComment() {
if (!FacesContext.getCurrentInstance().isPostback()) {
// ...
}
}
The <f:event type="preRenderXxx">
(where Xxx
can be View
or Component
) 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. <f:event type="preRenderXxx">
(其中Xxx
可以是View
或Component
)本质上是一种“变通方法”,用于在视图参数被调用之后能够调用bean动作方法的功能要求在初始请求中处理。 In the upcoming JSF 2.2 a new <f:viewAction>
tag will be introduced which should do exactly the job as intented: 在即将推出的JSF 2.2中,将引入一个新的
<f:viewAction>
标签,它应该完全符合<f:viewAction>
的工作:
<f:viewAction action="#{newsBean.sendComment}" />
This tag supports an onPostback
attribute which already defaults to false
: 此标记支持
onPostback
属性,该属性已默认为false
:
<f:viewAction action="#{newsBean.sendComment}" onPostback="false" />
JSF 2.2 will be released the first quart of 2012. Snapshot releases of JSF 2.2 are currently already available. JSF 2.2将于2012年第一季度发布.JSF 2.2的快照版本目前已经发布。
I guess your <f:event>
tag is put inside the <h:form>
tag. 我想你的
<f:event>
标签放在<h:form>
标签内。 Hence, when you click the ajax button, it re-render the whole <h:form>
component which results in the preRenderComponent event being triggered again. 因此,当您单击ajax按钮时,它会重新呈现整个
<h:form>
组件,从而导致再次触发preRenderComponent事件。
I think what you should use is PreRenderViewEvent . 我认为你应该使用的是PreRenderViewEvent 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.