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

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

DDD
DDDOriginal
2024-10-25 04:07:20566browse

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

Passing Parameters to p:remoteCommand from JavaScript

In this scenario, you seek to pass values to your p:remoteCommand from JavaScript. Here's how it can be achieved, depending on your PrimeFaces version:

PrimeFaces 3.3 and Newer

Since version 3.3, the syntax has evolved. As per the PrimeFaces user guide, here's how to pass parameters:

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

In the backing bean, access these parameters through the @ManagedProperty annotation:

<code class="java">@ManagedProperty("#{param.x}")
private int x;

@ManagedProperty("#{param.y}")
private int y;</code>

PrimeFaces 3.2 and Older

Before version 3.3, the syntax was different:

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

Retrieval of these parameters in the backing bean remains the same as described above.

Additional Notes

Alternatively, you can fetch parameters using FacesContext in a broader scoped bean:

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

For passing multiple values to a single parameter, use the following syntax:

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

Access these values in the backing bean using @ManagedProperty or FacesContext:

<code class="java">@ManagedProperty("#{paramValues.foo}")
private String[] foos;</code>

Related Resources

  • [Dependency injection of request parameters with CDI and JSF2](https://stackoverflow.com/questions/16025098/dependency-inject-request-parameter-with-cdi-and-jsf2)
  • [Invoking a JSF managed bean event with native JavaScript](https://stackoverflow.com/questions/7120603/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 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