In the Primefaces User Guide it shows examples of how to make AJAX calls to the server
PrimeFaces.ajax.AjaxRequest('/myapp/createUser.jsf',
{
formId: 'userForm',
oncomplete: function(xhr, status) {alert('Done');}
});
What I can't figure out is how to call a particular method. My goal is to invalidate the session from the client using JavaScript.
RemoteCommand is a nice way to achieve that because it provides you a JavaScript function that does the stuff (calling backing bean, refreshing, submitting a form etc., everything that command link can do).
From PrimeFaces 3.4 documentation:
<p:remoteCommand name="increment" actionListener="#{counter.increment}"
out="count" />
<script type="text/javascript">
function customFunction() {
//your custom code
increment(); //makes a remote call
}
</script>
What I've typically done is put a hidden p:commandLink on the page, then have Javascript call the click() event on it.
<p:commandLink id="hiddenLink"
actionListener="#{bean.methodToInvoke}" style="display:none"/>
Then
$('#hiddenLink').click();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With