Home >Web Front-end >JS Tutorial >How to Pass Parameters to p:remoteCommand in PrimeFaces from JavaScript?
Passing Parameters to p:remoteCommand from JavaScript
The p:remoteCommand component in PrimeFaces provides a convenient way to execute server-side actions from JavaScript. Passing parameters to the remote command is possible, enabling dynamic data transfer between the client and server.
PrimeFaces 3.3 and Newer
In PrimeFaces 3.3 and newer versions, the syntax for passing parameters to p:remoteCommand has changed. You can specify multiple values for a single parameter using the following syntax:
functionName([{name:'x', value:10}, {name:'y', value:20}]);
In the backing bean, you can access these parameters using "@ManagedProperty" or via request parameter maps:
@ManagedProperty(value = "#{param.x}") private int x; @ManagedProperty(value = "#{param.y}") private int y;
<code class="java">Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); int x = Integer.valueOf(params.get("x")); int y = Integer.valueOf(params.get("y"));</code>
PrimeFaces 3.2 and Older
In PrimeFaces 3.2 and older versions, the syntax for passing parameters was as follows:
increment({param1:'val1', param2:'val2'});
In the backing bean, you can access parameters similarly as described for PrimeFaces 3.3 or newer.
Note for Multiple Values
Before PrimeFaces 3.3, it was not possible to pass multiple values for a single parameter. To overcome this limitation, you could use the following syntax in PrimeFaces 3.3 or newer:
functionName([{name:'foo', value:'one'}, {name:'foo', value:'two'}, {name:'foo', value:'three'}]);
In the backing bean, you can access multiple values for a parameter using "@ManagedProperty" or via request parameter value maps:
@ManagedProperty(value = "#{paramValues.foo}") private String[] foos;
<code class="java">Map<String, String[]> paramValues = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap(); String[] foos = paramValues.get("foo");</code>
The above is the detailed content of How to Pass Parameters to p:remoteCommand in PrimeFaces from JavaScript?. For more information, please follow other related articles on the PHP Chinese website!