问题描述
我正在使用primefaces-4.0构建一个Web应用程序.我想在单击命令按钮时同时调用两个bean方法.我通过使用 remoteCommand 进行了尝试.
I am building a web application using primefaces-4.0. I wanted to call two bean methods simultaneously on click of command button. I tried it by using remoteCommand.
<p:commandButton value="Submit" ajax="false"
actionListener="#{userBean.execute}"
onclick="callCorrelation()">
</p:commandButton>
<p:remoteCommand name="correlation" update="correlationDialog"
actionListener="#{userBean.correlation}" />
Java脚本功能:
<head>
<script type="text/javascript">
$(document).callCorrelation(function() {
correlation ();
});
</script>
</head>
但这没用.
还有其他方法可以同时调用两个bean方法吗?
Is there any other way to call two bean methods simultaneously?
推荐答案
您的具体问题是由于您已通过ajax="false"
关闭ajax而引起的.这将创建一个同步表单提交,这将无法触发ajax请求.如果删除ajax="false"
,则可能会起作用,但是如果一种方法取决于另一种方法的结果,则您仍然处于竞争状态.还没有定义先执行哪个.
Your concrete problem is caused because you've turned off ajax by ajax="false"
. This will create a synchronous form submit which makes it impossible to fire an ajax request along. If you remove ajax="false"
, then it will likely work, but you've still a race condition if the one method depends on the result of the other. It's not defined which one would be executed first.
最好只使用一个命令组件.您可以同时使用action
和actionListener
. action
用于业务操作. actionListener
用于准备业务操作.如果您需要更多的动作侦听器,则只需嵌套<f:actionListener>
或<f:setPropertyActionListener>
.
Better just use a single command component. You can use action
and actionListener
together. The action
is intented for business actions. The actionListener
is intented for preparing of business actions. If you need more action listeners, just nest a <f:actionListener>
or perhaps a <f:setPropertyActionListener>
.
<p:commandButton value="Submit"
actionListener="#{userBean.correlation}"
action="#{userBean.execute}"
update="correlationDialog" />
另请参见:
- action和actionListener之间的差异
- Differences between action and actionListener
See also:
这篇关于同时在primeface中调用多个bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!