Skip to content

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

PrimesFaces: send Javascript JSON to the Managed Bean

In order to send to a Managed Bean a JSON javascript variable (or any String variable), a remoteCommand method should be called using the following syntax, in XHTML:


<!-- javascript calling the remoteCommand-->
<script>
jsonString = JSON.stringify(jsonObject);
jsMethod([{name: 'paramName',value: jsonString},{name: 'paramName2',value: anyStringVariable}]
</script>
<p:remoteCommand name="jsMethod" action="#{myBean.actionMethod}" update="@form">
</p:remoteCommand>

Note that the jsonVariable could be also any String, you’re just looking for sending some javascript data to the Managed Bean.

On the server side, the Managed Bean may retrieve the variable as follow:


Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String jsonString= params.get("paramName");

Then you may use your favorite JSON parser (e.g. Gson) for parsing the JSON string.
Requirements:

  • PrimeFaces > 3.5
  • JSF 2

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.