Pass parameter to p:remoteCommand from JavaScript

Yes, it is possible. How to do that depends on the PrimeFaces version. You can see it in PrimeFaces users guide.

PrimeFaces 3.3 or newer

Since PrimeFaces version 3.3 the syntax is as follows (copypasted from 3.3 users guide).

3.81 RemoteCommand

Passing Parameters

Remote command can send dynamic parameters in the following way;

increment([{name:'x', value:10}, {name:'y', value:20}]);

This way offers the possibility to specify multiple values on a single parameter name. Parameters with single values like above are available the same way as the old way:

@ManagedProperty("#{param.x}")
private int x;

@ManagedProperty("#{param.y}")
private int y;

(note: you can use Integer in Mojarra, but not in MyFaces, this is further completely unrelated to <p:remoteCommand>)

or in method of a broader scoped bean:

Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
int x = Integer.valueOf(params.get("x"));
int y = Integer.valueOf(params.get("y"));

If you need to specify a parameter with multiple values, then you could do it as follows:

functionName([{name:'foo', value:'one'}, {name:'foo', value:'two'}, {name:'foo', value:'three'}]);`

with in a request scoped bean:

@ManagedProperty("#{paramValues.foo}")
private String[] foos;

or in method of a broader scoped bean:

Map<String, String[]> paramValues = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap();
String[] foos = paramValues.get("foo");

PrimeFaces 3.2 or older

Before PrimeFaces version 3.3 the syntax is as follows (copypasted from 3.2 users guide):

3.80 RemoteCommand

Passing Parameters

Remote command can send dynamic parameters in the following way;

increment({param1:'val1', param2:'val2'});

It’s available in the backing bean by usual means. E.g. in a request scoped bean:

@ManagedProperty("#{param.param1}")
private String param1;

@ManagedProperty("#{param.param2}")
private String param2;

or in method of a broader scoped bean:

Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String param1 = params.get("param1");
String param2 = params.get("param2");

This approach had however the disadvantage that you can’t specify a single parameter with multiple values like as possible with normal HTML forms and HTTP request parameters (which is in real world used on e.g. multiple select dropdownlist and multiple select checkboxgroup).


See also:

Leave a Comment