我是 JSF、Primefaces 和 Ajax 的新手,所以我要做的是在我的后台 bean 上的验证为真时更新一个面板,并在它为假时更新另一个面板。
<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>
背 bean :
public void validate() {
...
if(validatecondition) {
// Update panel 1
} else {
// update panel 2
}
}
那么可以使用ajax来做到这一点吗?提前致谢!!
最佳答案
当然,两种方式。由于您使用的是primefaces,因此两个选项中较容易的一个是
public void validate() {
RequestContext context = RequestContext.getCurrentInstance();
if(validatecondition) {
context.update("panel1");
} else {
context.update("panel2");
}
}
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");
}
getRenderIds()
call 返回一个组件 Id 列表,JSF 将在响应完成时通过 ajax 更新这些 Id。这基本上是什么RequestContext
在primefaces 将在引擎盖下做。
关于ajax - Primefaces ajax 根据 backbean 结果更新不同的面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14779852/