根据backbean结果Primefaces AJAX更新不同的面板面板、根据、不同、结果

2023-09-10 14:37:41 作者:人潮拥挤我也会牵住你的手

我是新来的JSF,Primefaces和Ajax,所以我想要做的就是更新一个面板,如果在我的背上豆验证为真,并更新另一个面板时,它是假的。

I'm new to JSF, Primefaces and Ajax, so what i'm trying to do is update one panel if a validation on my back bean is true and update another panel when it's 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?在此先感谢!

So is it possible to do this using ajax? Thanks in advance!!

推荐答案

当然,两种方式。由于您使用primefaces,两个选项更容易将

Sure, two ways. Since you're using primefaces, the easier of two options would be

使用的RequestContext 反对选择性地更新面板。您的code将是这样的:

Use the RequestContext object to update the panels selectively. Your code will look like this:

 public void validate() {
   RequestContext context = RequestContext.getCurrentInstance();
   if(validatecondition) {
     context.update("panel1");
   } else {
     context.update("panel2");
   }
}

JSF PartialViewContext可以做同样的工作,有一点点打字

JSF PartialViewContext can do the same job, with just a little more typing

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()调用返回组件ID的JSF将通过AJAX更新响应完成的列表。这基本上是的RequestContext 在primefaces将引擎盖下做的。

The getRenderIds() call returns a list of component Ids that JSF will update via ajax on completion of the response. This is basically what RequestContext in primefaces will do under the hood.