Skip to content

Mis en ligne par admin - 22 janvier 2014 - Web Applications

PrimeFaces: send data from Managed Bean to Javascript

In order to send some object data from a Managed Bean to a client-side javascript function, the following approach should be followed.

Implement a listener/action method which return the data in JSON format parsed by Gson (or any other parser:


public void actionMethod() {
    Gson gson = new Gson();
    RequestContext.getCurrentInstance().addCallbackParam("mydata",
    gson.toJson(myObject));
  }

In your XHTML, any ajax component may be used to call the function, then the oncomplete function must call a function with the following parameters:


<script>
function parsingFunction(xhr, status, args) {
      var jsonObject= JSON.parse(args.mydata);
             //...
      }
</script>
<p:ajax event="change" listener="#{myBean.actionMEthod}"             oncomplete="parsingFunction(xhr,status,args)" />

Articles similaires

Les articles suivants abordent des thèmes communs.

Commentaires

Partagez vos remarques.

Il n'y a pas de commentaires pour cet article.

Add a Comment

Fill in the form and submit.