Home > Web Front-end > JS Tutorial > body text

How to Pass Parameters to p:remoteCommand in PrimeFaces from JavaScript?

Susan Sarandon
Release: 2024-10-24 14:01:31
Original
310 people have browsed it

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}]);
Copy after login

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;
Copy after login
<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>
Copy after login

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'});
Copy after login

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'}]);
Copy after login

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;
Copy after login
<code class="java">Map<String, String[]> paramValues = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap();
String[] foos = paramValues.get("foo");</code>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!