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

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

DDD
Release: 2024-10-25 04:07:20
Original
560 people have browsed it

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

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

PrimeFaces 3.2 and Older

Before version 3.3, the syntax was different:

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

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

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

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

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

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!

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