从javascript中获取托管bean的返回值 [英] Get the returned value from managed bean in javascript
问题描述
在我的应用程序中,我调用一个Javascript事件,该事件调用 p:remoteCommand
名为 checkPageLayoutsAreSelected
,如下所示:
In my application I call a Javascript event which calls a p:remoteCommand
named checkPageLayoutsAreSelected
as following :
$('selector').on('click', function (e) {
checkPageLayoutsAreSelected();
});
这是 p:remoteCommand
:
<p:remoteCommand name="checkPageLayoutsAreSelected" actionListener="#{beanFormDashboard.checkPageLayoutsAreSelected}" />
此 p:remoteCommand
将调用方法在 beanFormDashboard
托管bean中,它将返回一个布尔值:
This p:remoteCommand
will call a method in the beanFormDashboard
managed bean which will return a Boolean value :
public Boolean checkPageLayoutsAreSelected(){
for(DashboardPage dp : dashboardPageList){
if(dp.getModel() == 0){
return false;
}
}
return true;
}
所以我想通过获取返回值来自Javascript代码中托管bean的checkPageLayoutsAreSelected()
。
这样的事情:
$('selector').on('click', function (e) {
var returnedValue = checkPageLayoutsAreSelected();
});
我该怎么做?
推荐答案
checkPageLayoutsAreSelected
不会返回值甚至是promise,但您可以Ajaxicaly返回值。
checkPageLayoutsAreSelected
doesn't return a value or even a promise but you can Ajaxicaly return value.
<p:remoteCommand name="checkPageLayoutsAreSelected"
action="#{beanFormDashboard.checkPageLayoutsAreSelected()}"
oncomplete="getLayoutAreSelectedResult(xhr, status, args);"
/>
在方法 checkPageLayoutsAreSelected()
你使用PF提供的RequestContext将结果发送回客户端:
And in the method checkPageLayoutsAreSelected()
you use RequestContext provided by PF to send result back to client:
public void checkPageLayoutsAreSelected() {
Boolean result=true;
for(DashboardPage dp : dashboardPageList){
if(dp.getModel() == 0){
result= false;
}
}
RequestContext reqCtx = RequestContext.getCurrentInstance();
reqCtx.addCallbackParam("returnedValue", result);
}
在Javascript回调函数中 getLayoutAreSelectedResult(xhr, status,args)
你将得到返回值:
And in the Javascript callback function getLayoutAreSelectedResult(xhr, status, args)
you will have the returned value:
$('selector').on('click', function (e) {
checkPageLayoutsAreSelected();
window.getLayoutAreSelectedResult= function(xhr, status, args) {
var returnedValue = args.returnedValue;
console.log(returnedValue);
}
});
这篇关于从javascript中获取托管bean的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!