JSF2从Backing Bean通过AJAX获取JSON数据

时间:2022-09-17 10:54:28

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.

我正在使用RichFaces 4.我使用flot作为JQuery Charting库,我想实现一个图表,通过AJAX不断刷新自己。

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

我从我的支持bean通过JSON获取图表的数据。

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.

我迷失在如何从支持bean获取新数据到javascript变量。据我所知,我不能使用 ,因为没有用户交互,也没有使用html输入元素。

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.

我认为解决方案是使用jsf.ajax.request。但我不明白如何在没有输入元素的情况下调用它。

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.

下面是一个代码剪辑它应该是什么样子,但我不知道如何从我的支持bean获取值到javascript变量。

$(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).

我在使用一些input-element时设法用jsf.ajax.request发送AJAX请求,但我没有得到渲染的新值。即使我可以,我不会知道热的javascript变量(我没有很多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.

注意:我将javax.faces.SEPARATOR_CHAR更改为“ - ”,因为我需要“:”来表示jquery。

Thanks for your help!

谢谢你的帮助!

1 个解决方案

#1


0  

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.

JSF是这项工作的错误工具。使用像JAX-RS这样的Web服务框架,而不是像JSF这样的MVC框架。然后你可以在客户端以通常的方式使用jQuery等等。

See also:

#1


0  

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.

JSF是这项工作的错误工具。使用像JAX-RS这样的Web服务框架,而不是像JSF这样的MVC框架。然后你可以在客户端以通常的方式使用jQuery等等。

See also: