Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Primefaces JavaScript to call a JSF method on a bean on the server

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.

like image 893
Mark Stang Avatar asked Jan 25 '11 18:01

Mark Stang


2 Answers

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>
like image 177
Danubian Sailor Avatar answered Nov 06 '22 20:11

Danubian Sailor


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();
like image 21
wrschneider Avatar answered Nov 06 '22 19:11

wrschneider