问题
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 Script function :
<head>
<script type="text/javascript">
$(document).callCorrelation(function() {
correlation ();
});
</script>
</head>
But it did not work.
Is there any other way to call two bean methods simultaneously?
回答1:
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.
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" />
See also:
- Differences between action and actionListener
来源:https://stackoverflow.com/questions/18850322/call-multiple-bean-method-in-primefaces-simultaneously