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

How to Pass Parameters to p:remoteCommand from JavaScript?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-24 11:41:02499browse

How to Pass Parameters to p:remoteCommand from JavaScript?

Passing Parameters to p:remoteCommand from JavaScript

Question:

Is it possible to pass values to the p:remoteCommand component from JavaScript? If so, how can these values be received in the backing bean?

Answer:

Yes, passing parameters to p:remoteCommand from JavaScript is possible. The approach depends on the PrimeFaces version being used.

PrimeFaces 3.3 or Newer

For PrimeFaces 3.3 and newer, use the following syntax:

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

This allows for multiple values with the same parameter name. Parameters with single values can be accessed in the backing bean as request-scoped properties:

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

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

PrimeFaces 3.2 or Older

For PrimeFaces 3.2 or older, use the following syntax:

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

These parameters can be accessed in the backing bean using the RequestParameterMap:

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

Passing Parameters with Multiple Values

To pass parameters with multiple values, use the following syntax (PrimeFaces 3.3 or newer):

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

This can be accessed in the backing bean as a managed property with the paramValues attribute:

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

Additional Resources

  • [Dependency Inject Request Parameter with CDI and JSF2](https://stackoverflow.com/questions/2644660/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/1840238/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 Parameters to p:remoteCommand from JavaScript?. 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