Home  >  Article  >  Web Front-end  >  How to Pass Values to p:remoteCommand from JavaScript in PrimeFaces?

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

Linda Hamilton
Linda HamiltonOriginal
2024-10-24 22:02:30453browse

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>

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;

PrimeFaces 3.2 or Older

Before PrimeFaces 3.3, the syntax was:

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

In the backing bean, parameters can be accessed through:

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

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

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>

These parameters can be accessed in a request scoped bean:

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

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>

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!

Statement:
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