I am trying to execute a JSF2 bean method and show a dialog box after completion of the method on click of PrimeFaces <p:commandButton>
.
<p:commandButton id="viewButton" value="View"
actionlistener="#{userBean.setResultsForSelectedRow}" ajax="false"
update=":selectedRowValues"
oncomplete="PF('selectedRowValuesDlg').show()">
</p:commandButton>
<p:dialog id="selectedRowValues" widgetVar="selectedRowValuesDlg" dynamic="true">
<h:outputText value="#{userBean.selectedGroupName}" />
</p:dialog>
When I click on the command button, the bean action listener method setResultsForSelectedRow
executes properly, but it does not show the dialog box when the method completes. If I remove actionlistener
, it shows the dialog box. I do not know what is going wrong.
What is the execution order of events? Is it possible to execute actionlistener
and oncomplete
simultaneously?
Best Answer
It failed because you used
ajax="false"
. This fires a full synchronous request which in turn causes a full page reload, causing theoncomplete
to be never fired (note that all other ajax-related attributes likeprocess
,onstart
,onsuccess
,onerror
andupdate
are also never fired).That it worked when you removed
actionListener
is also impossible. It should have failed the same way. Perhaps you also removedajax="false"
along it without actually understanding what you were doing. Removingajax="false"
should indeed achieve the desired requirement.No. The script can only be fired before or after the action listener. You can use
onclick
to fire the script at the moment of the click. You can useonstart
to fire the script at the moment the ajax request is about to be sent. But they will never exactly simultaneously be fired. The sequence is as follows:onclick
JavaScript code is executedprocess
and current HTML DOM treeonstart
JavaScript code is executedprocess
actionListener
JSF backing bean method is executedaction
JSF backing bean method is executedupdate
and current JSF component treeonsuccess
JavaScript code is executedonerror
JavaScript code is executedupdate
based on ajax response and current HTML DOM treeoncomplete
JavaScript code is executedNote that the
update
is performed afteractionListener
, so if you were usingonclick
oronstart
to show the dialog, then it may still show old content instead of updated content, which is poor for user experience. You'd then better useoncomplete
instead to show the dialog. Also note that you'd better useaction
instead ofactionListener
when you intend to execute a business action.See also: