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"
    action="#{myBean.someAction}"
    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" />



Done!!

6 comments:







  1. 22.04.2014 2:17:51 com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
    SEVERE: Error Rendering View[/test.xhtml]
    javax.el.ELException: /resources/keremet/sendPoint.xhtml: Property 'method' not found on type com.keremet.SendPointBean
    at com.sun.faces.facelets.compiler.AttributeInstruction.write(AttributeInstruction.java:94)

    ReplyDelete
    Replies
    1. p.s.

      I used code:



      Delete
    2. Hi,

      there are some HTML reserved characters in your code.
      use the HTML encoder to encode them first.
      http://www.opinionatedgeek.com/DotNet/Tools/HTMLEncode/encode.aspx

      Delete
  2. stupid blog coudn't view the code I insert into comment!

    ReplyDelete
  3. thanks a lot!
    you should work for primefaces docu team. They really need good people ;)

    ReplyDelete
  4. Nice tip. Thanks for that.

    ReplyDelete

LinkWithin

Related Posts Plugin for WordPress, Blogger...