我可以从JSF支持bean方法更新JSF组件吗?

Bes*_*ces 96 primefaces jsf-2

有没有办法让JSF Backing bean导致页面上组件的更新?我不打算使用带有update属性的ajax组件来更新页面上的组件.我需要从JSF支持bean方法中触发更新.请注意,此方法完成后或完成之前,页面上的更新可能会发生.我正在使用PrimeFaces,如果有一个解决方案可以使用PrimeFaces.

Bal*_*usC 170

使用标准JSF API,将客户端ID添加到PartialViewContext#getRenderIds().

FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("foo:bar");
Run Code Online (Sandbox Code Playgroud)

使用PrimeFaces特定的API,使用RequestContext#update().

RequestContext.getCurrentInstance().update("foo:bar");
Run Code Online (Sandbox Code Playgroud)

从PrimeFaces 6.2 RequestContext#update()已被弃用,请PrimeFaces::Ajax#update改用.

PrimeFaces.current().ajax().update("foo:bar");
Run Code Online (Sandbox Code Playgroud)

如果您碰巧使用JSF实用程序库OmniFaces,请使用Ajax#update().

Ajax.update("foo:bar");
Run Code Online (Sandbox Code Playgroud)

不管方式如何,请注意那些客户端ID应该表示绝对客户端ID,这些客户端ID NamingContainer分隔符为前缀,就像在视图侧一样.

  • 不,现在为时已晚.移动作业以调用操作阶段或渲染响应阶段之前.通过`<f:ajax listener>`或`<f:event type ="preRenderView"listener>调用方法 (8认同)
  • 在渲染响应阶段(不幸的是,我被迫放置此代码的地方)似乎无法使其工作.如果我放在Invoke Application或早期阶段,事情就可以了.处于渲染响应阶段的任何变通方法? (6认同)

Fri*_*fer 9

我还尝试从jsf支持bean /类更新组件

操作UI组件后,您需要执行以下操作:

FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add(componentToBeRerendered.getClientId())
Run Code Online (Sandbox Code Playgroud)

使用clientId而不是(服务器端)componentId是很重要的!!


ℛɑƒ*_*ƒæĿ 7

RequestContext弃用Primefaces 6.2.从此版本使用以下内容:

if (componentID != null && PrimeFaces.current().isAjaxRequest()) {
    PrimeFaces.current().ajax().update(componentID);
}
Run Code Online (Sandbox Code Playgroud)

并且从backbean 执行javascript使用这种方式:

PrimeFaces.current().executeScript(jsCommand);
Run Code Online (Sandbox Code Playgroud)

参考: