JSF2, PrimeFaces and Highcharts - Ajax

Has anyone used JSF2, PrimeFaces and Highcharts together? I really got confused about how I should combine all this, especially regarding the ajax request, in order to get data from the server to submit to Highcharts in the view for updating the chart.

Now I have a servlet that processes an Ajax request, which is sent using the JQuery.ajax () method and uses JQuery to update the chart with new data received as a JSON object. And I use GSon.toJSon to convert a Java object to a JSON object.

What I'm trying to achieve here is that I want to replace this servlet with JSF2. Instead of using another servlet, I want to use JSF and have some bean support for preparing and sending the JSON object to the client.

Is anyone

+6
source share
1 answer

In the example below, the p: commandButton command starts an ajax request. The JSON object you want to use can be stored in the h: inputHidden field. When p: commandButton exits, the javascript function is called to update the chart. The javascript function will be able to access the JSON object from the h: inputHidden field.

XHTML

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.prime.com.tr/ui"> <h:head> <script type="text/javascript"> function dosomething() { var value = jQuery("#beanvalue").attr('value'); alert(value); } </script> </h:head> <h:body> <h:form prependId="false" > <p:commandButton value="update" action="#{testBean.update}" update="beanvalue" oncomplete="dosomething();" /> <h:inputHidden value="#{testBean.output}" id="beanvalue" /> </h:form> </h:body> </html> 

Bean

 import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; @ManagedBean @ViewScoped public class TestBean { private String output; public TestBean() { output = "1"; } public void update() { output += "1"; } public String getOutput() { return output; } public void setOutput(String output) { this.output = output; } } 
+7
source

Source: https://habr.com/ru/post/888068/


All Articles