15

For passing paramters from JS to p:remoteCommand(provided by primefaces), you can use this:

remoteCommandFunctionName({name1:'value1', name2:'value2'});

After that, how do you receive this set of parameters in remoteCommand for sending it to backing beans?

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294
  • 1
    The answer here are outdated, please see http://stackoverflow.com/a/18510102/55070 – leo Apr 29 '15 at 13:29

4 Answers4

18

Shamelessly plug my answer because it costs me hours to solve this problem in PrimeFace 3.3. The solution is to pass your arguments as an array of {name: <param-name>, value: <param-value>}.

As in Neyko's answer, the invocation should be changed to:

remoteCommandFunctionName([{name: 'name1', value: 'value1'}, {name: 'name2', value: 'value2'}]);
instcode
  • 1,495
  • 14
  • 16
  • Works in primfaces 3.4.2 now. Migrated from 3.2 to 3.4.2 and invocation stopped. Neyko's answer should be updated as well. – Vivek Apr 17 '13 at 11:34
  • Yep sure am glad i read down further and got to this comment. – Nicholas DiPiazza Sep 01 '13 at 07:24
  • @instcode has the right answer and if (like me) you didn't understand it the first time, let's be more explicit : You have to textually write "name:" and "value:" because they are keywords. You cannot replace name: with your custom name (name1, for instance...). Of course, what goes right of the colon can be a js variable or a 'constant'. Thanks again! You solve my problem! – Pierre C Sep 12 '14 at 03:21
17

If you have defined remote command like this:

<p:remoteCommand name="remoteCommandFunctionName" 
                 actionListener="#{myBean.exec}"/>

And you have a Javascript method call with parameters:

remoteCommandFunctionName({name1:'value1', name2:'value2'});

You don't need to specify parameters passed to Javascript method call into remoteCommand. I think that you'll need these parameters in the backing bean after all. You can use the request parameters map to obtain the values for the parameters passed in the JavaScript call in the backing bean method:

public void exec() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map map = context.getExternalContext().getRequestParameterMap();
    String name1 = (String) map.get("name1");
    String name2 = (String) map.get("name2");
}
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
Neyko
  • 179
  • 2
  • 7
  • 1
    and how exactly do you pass those request parameters through javascript ? – Rajat Gupta Jan 04 '12 at 20:17
  • 1
    Actually I haven't found enough information about using the javascript passed parameters into the backing bean. The information I'm giving here is related to my experience in working with PrimeFaces remoteCommand. May be there are better ways of implementation. – Neyko Jan 05 '12 at 08:18
  • 1
    Sidenote: getRequestParameterMap() is a Map, if you add these type arguments to the Map, you dont need to cast. – Jonathan Viccary Aug 25 '14 at 14:33
10

Solution from instcode works in primefaces 4.0

xhtml

<p:remoteCommand name="remoteCommandFunctionName" actionListener="#{myBean.exec}"/>

Bean

public void exec() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map map = context.getExternalContext().getRequestParameterMap();
    String name1 = (String) map.get("name1");
    String name2 = (String) map.get("name2");
}

JavaScript

remoteCommandFunctionName([{name: 'name1', value: 'value1'}, {name: 'name2', value: 'value2'}]);
Leon Borko
  • 198
  • 2
  • 13
6

I tried the previous solutions with primefaces 4.0 but they didn't work for me.

So as a workaround I had to put a <h:inputHidden> and set the value to a property of a ManagedBean, and just before calling the <p:remoteCommand> I set the value of this h:inputHidden (using jQuery) and call the p:remoteCommand (with making sure the remote command is processing the h:inputHidden)

FormBean.java

@ManagedBean(name = "formBean")
@ViewScoped
public class FormBean {

  private String myValue;

  public String getMyValue() {
   return myValue;
  }

  public void setMyValue(String myValue) {
      this.myValue = myValue;
  }

  public void remoteAction() {
     someAction(myValue);
  }

}

form.xhtml

.....
<p:remoteCommand name="remoteAction" actionListener="#{formBean.remoteAction()}" process="@this myValueHidden" />

<h:inputHidden id="myValueHidden" value="#{formBean.myValue}" />
.....

form.js

function onClickOfSomeButton() {
$('#formName\\:myValueHidden').val('myValue test value');
  remoteAction();   
}

EDIT

Also this works perfectly..

remoteAction([{name: 'name1', value: 'value1'}, {name: 'name2', value: 'value2'}]);

Hope this helps...

Grégoire C
  • 1,361
  • 1
  • 13
  • 32
Hatem Alimam
  • 9,968
  • 4
  • 44
  • 56