I’ve been doing a ton of PrimeFaces JSF development over the last little while and that library is one stunning piece of engineering execution. That said, I’m still a JSF noob, and one thing I keep having to look up is how to invoke backend JSF beans from JavaScript using p:remoteCommand. It’s covered in the Primefaces manual, but not in the standard google-able samples AFAIK.

It even ended up in this bug submission, but it’s not really a bug, just something that should be documented in a blog somewhere for quick googling.

Brother Glen will now school you on everything you need to know.

First, write the backing bean Java code to handle the parameters you get invoked from your JS client. You’ll have to parse our the args from a RequestParameterMap, but life is still good…

public void onBrowserStuff() {
    Map<String, String> requestParamMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    if (requestParamMap.containsKey("yourArg")) {
        String yourValue = requestParamMap.get("yourArg");
        // knock yourself out with yourValue
    }
}

Then, put a p:remoteCommand in your markup so you can invoke that backing method from your JavaScript:

<p:remoteCommand name="onBrowserStuff" actionListener="#{yourBean.onBrowserStuff}"/>

Finally, call the thing with whatever args you may require. These args need to be in an array of name/value hashes, so watch your brackets and braces:

<script type="text/javascript">

$(document).ready(fuction() {

    onBrowserStuff( [ { name: 'yourArg', value: 'yourValue' }, { name: 'yourOtherArg', value: 'yourOtherValue' } ] );

));

</script>

&nbsp;

There, I’ve now written down, so I can google it for myself next time.