简体   繁体   中英

JSF2 get JSON Data via AJAX from Backing Bean

I am using RichFaces 4. I am using flot as a JQuery Charting library and i want to implement a chart, that keeps refreshing itself via AJAX.

I get the data for the chart via JSON from my backing bean.

I am lost on how to get new data from a backing bean into a javascript variable. From what i understand i can't use <f:ajax> since there is no user interaction and no use of html input elements.

I think the solution would be the use of jsf.ajax.request . But i don't understand how to call that without an input-element either.

Following is a code snipped of how it should look like, but i don't know how to get the value from my backing bean into a javascript variable.

$(function() {
function update(){
    //**dont know how this call should look like**
    jsf.ajax.request('dev-form-input1', {render:'dev-form-output1'});
    //'newAjaxData' would be the value from #{someBean.chartData}
    plot.setData(newAjaxData);
    // draw the chart with the new data
    plot.draw();
    //get an update every second
    setTimeout(update, 1000);
}

I managed to send AJAX requests with jsf.ajax.request when using some input-element, but i didnt get the new value to render. And even if i could i wouldnt know hot to get it in a javascript variable (i dont have a lot of experience with javascript).

<h:form id="dev-form">
<h:inputText id="input1" value="#{someBean.chartData}"/>
<h:outputText id="output1" value="#{someBean.chartData}" />
</h:form>

note: i changed the javax.faces.SEPARATOR_CHAR to "-", because i need ":" for jquery.

Thanks for your help!

JSF is the wrong tool for the job. Use a webservice framework like JAX-RS, not a MVC framework like JSF. Then you can in the client end just use jQuery the usual way and so on.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM