gpt4 book ai didi

ajax - 当 f :ajax is executed 时总是调用 JSF2 preRenderComponent

转载 作者:行者123 更新时间:2023-12-04 17:26:20 24 4
gpt4 key购买 nike

我有一个由 NewsBean.java 支持的 JSF 页面,其中有 <f:event type="preRenderComponent" listener="#{newsBean.init}" />作为 bean 初始值设定项。

页面底部有一个用于发送评论的按钮,该按钮具有:<f:ajax event="click" execute="@form" render="@form" listener="#{newsBean.sendComment}" />并被 <h:form> 包围.单击按钮时,NewsBean.init()总是被调用。

我的 bean 范围是 View 。这是一个有效的行为(总是调用 init())吗?我怎样才能防止总是打电话 init() ?

最佳答案

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

public void sendComment() {
if (!FacesContext.getCurrentInstance().isPostback()) {
// ...
}
}
<f:event type="preRenderXxx"> (其中 Xxx 可以是 ViewComponent )顺便说一句,本质上是一种“解决方法”,用于在初始请求处理 View 参数后能够调用 bean 操作方法的功能需求.在即将到来的 JSF 2.2 中,一个新的 <f:viewAction>将引入标签,它应该完全按照预期完成工作:
<f:viewAction action="#{newsBean.sendComment}" />

此标签支持 onPostback已经默认为 false 的属性:
<f:viewAction action="#{newsBean.sendComment}" onPostback="false" />

JSF 2.2 将于 2012 年第一季度发布。JSF 2.2 的快照版本目前已经可用。

关于ajax - 当 f :ajax is executed 时总是调用 JSF2 preRenderComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8558657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com