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

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

Linda Hamilton
Release: 2024-10-24 22:02:30
Original
452 people have browsed it

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

Passing Parameters to p:remoteCommand from JavaScript

Question:

Is it possible to pass values to a PrimeFaces p:remoteCommand from JavaScript, and how can it be done?

Answer:

Yes, it is possible to pass parameters to a p:remoteCommand from JavaScript. The syntax and approach depend on the PrimeFaces version:

PrimeFaces 3.3 or Newer

Since PrimeFaces 3.3, the syntax has changed as follows:

<code class="javascript">increment([{name:'x', value:10}, {name:'y', value:20}]);</code>
Copy after login

This allows for passing multiple values for a parameter name. Single-valued parameters can still be accessed as before:

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

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

PrimeFaces 3.2 or Older

Before PrimeFaces 3.3, the syntax was:

<code class="javascript">increment({param1:'val1', param2:'val2'});</code>
Copy after login

In the backing bean, parameters can be accessed through:

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

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

Multiple Value Parameters

In PrimeFaces 3.3 or newer, it is possible to specify parameters with multiple values, as in:

<code class="javascript">functionName([{name:'foo', value:'one'}, {name:'foo', value:'two'}, {name:'foo', value:'three'}]);</code>
Copy after login

These parameters can be accessed in a request scoped bean:

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

Or in a method of a broader scoped bean:

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

Additional Resources:

  • [Dependency Inject Request Parameter with CDI and JSF2](https://stackoverflow.com/questions/7941333/dependency-inject-request-parameter-with-cdi-and-jsf2)
  • [How to invoke a JSF managed bean on a HTML DOM event using native JavaScript?](https://stackoverflow.com/questions/9169658/how-to-invoke-a-jsf-managed-bean-on-a-html-dom-event-using-native-javascript)

The above is the detailed content of How to Pass Values to p:remoteCommand from JavaScript in PrimeFaces?. 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!