我是 JSF、Primefaces 和 Ajax 的新手,所以我想要做的是,如果我的 back bean 上的验证为 true,则更新一个面板;如果为 false,则更新另一个面板。
<h:panelGroup id="panel1">
...
<h:commandButton id="btn1" action="#{bean.validate}">
<p:ajax process="panel1" update="panel1"/>
</h:commandButton>
</h:panelGroup>
<h:panelGroup id="panel2">
...
</h:panelGroup>
后豆:
public void validate() {
...
if(validatecondition) {
// Update panel 1
} else {
// update panel 2
}
}
那么使用ajax可以做到这一点吗?提前致谢!!
当然,有两种方法。由于您使用的是 primefaces,因此两个选项中更简单的一个是
-
Use the 请求上下文反对有选择地更新面板。您的代码将如下所示:
public void validate() {
RequestContext context = RequestContext.getCurrentInstance();
if(validatecondition) {
context.update("panel1");
} else {
context.update("panel2");
}
}
-
JSF PartialViewContext可以做同样的工作,只需多打字一点
FacesContext ctxt = FacesContext.getCurrentInstance(); //get your hands on the current request context
if(validatecondition) {
ctxt.getPartialViewContext().getRenderIds().add("panel1");
} else {
ctxt.getPartialViewContext().getRenderIds().add("panel2");
}
The getRenderIds()
调用返回一个组件 ID 列表,JSF 将在响应完成时通过 ajax 更新该列表。基本上就是这样RequestContext
在 primefaces 中就可以在幕后进行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)