Saturday, September 7, 2013

How to pass parameters with p:remoteCommand

In some cases, we might want to invoke an server side action via java script.
This can be done by <p:remoteCommand />.
with <p:remoteCommand />, we could assign a name to the remoteCommand component, then invoke the remoteCommand's action with the given name
<p:remoteCommand name="rcTest"
    update="any_component_id" />

<p:commandButton value="remote command test" onclick="rcTest();" />

In Addition, we might want to pass in some parameters to our server side action with the following syntax.
<p:commandButton value="remote command test" 
    onclick="rcTest([{name:'myName1',value:'myValue1'},{name:'myName2',value:myValue2}]);" />

Request the passed in parameters' value
public void someAction() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String,String> params = context.getExternalContext().getRequestParameterMap();
    String myName1 = params.get("myName1");
    String myName2 = params.get("myName2");

The idea here is to construct the <f:param />
=> <f:param name="myName1" value="myValue1" />



Related Posts Plugin for WordPress, Blogger...