You can use the PrimeFaces remote command component ( <p:remoteCommand> ).
RemoteCommand allows you to back up the bean and perform a partial upgrade caused by the user client side of the script.
Add it to the view as follows:
<p:remoteCommand name="myRemote" actionListener="#{myBean.listen}"/>
And use it in Javascript as follows:
<script type="text/javascript"> myRemote(); </script>
or call it from the event handler as follows:
<div onclick="myremote();">...</div>
If you want to pass parameters to the server, make the following call:
<script type="text/javascript"> myRemote([{name:'param1', value:150}, {name:'param2', value:220}]); </script>
The listener might look like this:
public void listen(){ FacesContext context = FacesContext.getCurrentInstance(); Map<String,String> params = context.getExternalContext().getRequestParameterMap(); System.out.println(params.get("param1")); System.out.println(params.get("param2")); }
One of the comments asked to return a value to Javascript.
In this case, you can use the Primeface query context execute () to execute whatever javascript you want.
RequestContext.getCurrentInstance().execute("your javascript code");
source share