在javascript中获取托管bean的返回值

Ich*_*aki 6 javascript jsf primefaces jsf-2 jsf-2.2

在我的应用程序中,我调用了一个 Javascript 事件,它调用了一个p:remoteCommand命名checkPageLayoutsAreSelected如下:

$('selector').on('click', function (e) {
  checkPageLayoutsAreSelected();
});
Run Code Online (Sandbox Code Playgroud)

这是p:remoteCommand

<p:remoteCommand name="checkPageLayoutsAreSelected" actionListener="#{beanFormDashboard.checkPageLayoutsAreSelected}" />
Run Code Online (Sandbox Code Playgroud)

p:remoteCommand将调用beanFormDashboard托管 bean中的一个方法,该方法将返回一个布尔值:

public Boolean checkPageLayoutsAreSelected(){
    for(DashboardPage dp : dashboardPageList){
        if(dp.getModel() == 0){
            return false;
        }
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

所以我想checkPageLayoutsAreSelected()从 Javascript 代码中的托管 bean 中获取返回值。

像这样的事情:

$('selector').on('click', function (e) {
  var returnedValue = checkPageLayoutsAreSelected();
});
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

fin*_*ich 6

checkPageLayoutsAreSelected 不返回值甚至承诺,但您可以 Ajaxicaly 返回值。

<p:remoteCommand name="checkPageLayoutsAreSelected"
     action="#{beanFormDashboard.checkPageLayoutsAreSelected()}"
     oncomplete="getLayoutAreSelectedResult(xhr, status, args);"
/>
Run Code Online (Sandbox Code Playgroud)

并且在checkPageLayoutsAreSelected()您使用 PF 提供的 RequestContext 将结果发送回客户端的方法中:

public void checkPageLayoutsAreSelected() {
   Boolean result=true;
   for(DashboardPage dp : dashboardPageList){
        if(dp.getModel() == 0){
            result= false;
        }
   }
   RequestContext reqCtx = RequestContext.getCurrentInstance();        
   reqCtx.addCallbackParam("returnedValue", result);
}
Run Code Online (Sandbox Code Playgroud)

在 Javascript 回调函数中,getLayoutAreSelectedResult(xhr, status, args)您将获得返回值:

$('selector').on('click', function (e) {
    checkPageLayoutsAreSelected();
    window.getLayoutAreSelectedResult= function(xhr, status, args) {
       var returnedValue = args.returnedValue;
       console.log(returnedValue);
    }
});
Run Code Online (Sandbox Code Playgroud)