PrimeFaces - Adding parameters to Ajax callback using RequestContext#addCallbackParam()

[Updated: Sep 23, 2017, Created: Sep 23, 2017]

This example shows how to use Primefaces RequestContext#addCallbackParam() method to add parameters for ajax oncomplete client side callbacks. The RequestContext#execute() method can embed basic params (String and primitives) as we saw in the last tutorial. RequestContext#addCallbackParam() has additional advantage of converting parameters of complex Java objects to JSONObject and JSONArray.


Example

JSF page

src/main/webapp/index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
<h:head>
    <script type="application/javascript">
     /* <![CDATA[ */
      function handleMsg(xhr, status, args){
       var message = args.msg;
       var number = args.number;
       var person = args.person;
       var personName = person.name;
       var personDob = person.dateOfBirth;

       var br = "<br/>";
       var str = "Msg:  "+message + br;
       str += "Number: " + number + br;
       str += "Person: " + person + br;
       str += "Person name: " + personName + br;
       str += "Person dob: " + personDob + br;
       $("#msgDiv").html(str);
    }
    /* ]]> */
    </script>
</h:head>
<h:body>
    <h2>RequestContext#addCallbackParam() Example</h2>
    <h:form>
        <p:commandButton value="Click Me"
                         actionListener="#{myBean.handleClick}"
                         oncomplete="handleMsg(xhr, status, args)"/>
    </h:form>
    <div style="font-size: 18px; padding: 20px;" id="msgDiv"></div>
</h:body>
</html>

Managed Bean

@ManagedBean
@ViewScoped
public class MyBean {

  public void handleClick(ActionEvent actionEvent) {
      Person person = new Person("Liz", new Date());
      RequestContext rc = RequestContext.getCurrentInstance();
      rc.addCallbackParam("msg", "some message");
      rc.addCallbackParam("number", 1);
      rc.addCallbackParam("person",  person);
  }
}
public class Person {
  private String name;
  private Date dateOfBirth;
    .............
}

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

Output

Accessing localhost:8080/index.xhtml and clicking on the button:



Example Project

Dependencies and Technologies Used :

  • primefaces 6.1 primefaces
  • jsf-api 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • JDK 1.8
  • Maven 3.3.9

RequestContext#addCallbackParam() example Select All Download
  • context-request-callback-param
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp

See Also