当执行 f:ajax 时,JSF2 preRenderComponent 总是被调用

发布于 2024-12-22 00:00:09 字数 440 浏览 3 评论 0原文

我有一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够钟 2024-12-29 00:00:09

preRender 侦听器始终在预渲染事件上调用,无论它是初始请求还是回发请求。每个请求都有一个渲染响应阶段,无论它是普通请求还是 ajax 请求。所以这种行为是符合规范的。您需要在侦听器方法中检查自己是否是回发请求,方法是检查 FacesContext#isPostback()

public void sendComment() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        // ...
    }
}

(其中 Xxx 可以是 ViewComponent)顺便说一句,本质上是一种“解决方法”,用于满足在初始请求处理视图参数后能够调用 bean 操作方法的功能要求。在即将推出的 JSF 2.2 中,将引入新的 标记,该标记应该完全按照预期完成工作:

<f:viewAction action="#{newsBean.sendComment}" />

该标记支持 onPostback 属性,该属性已默认为false

<f:viewAction action="#{newsBean.sendComment}" 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 checking 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. In the upcoming JSF 2.2 a new <f:viewAction> tag will be introduced which should do exactly the job as intented:

<f:viewAction action="#{newsBean.sendComment}" />

This tag supports an onPostback attribute which already defaults to 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.

蓝颜夕 2024-12-29 00:00:09

我猜你的 标签放在 标签内。因此,当您单击 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文