ajax JSF,使用ajax定期刷新组件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11228198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSF, refresh periodically a component with ajax?
提问by Potinos
I'm working on an application JSF and i want to refresh periodically a component with ajax Like facebook notification zone behaviour. How can i do that?
我正在开发一个应用程序 JSF,我想定期刷新一个组件,使用 ajax 就像 facebook 通知区域行为一样。我怎样才能做到这一点?
回答by Daniel
Poll is what you need to use
民意调查是你需要使用的
Poll component make ajax calls in a specified interval.
轮询组件在指定的时间间隔内进行 ajax 调用。
For example Primefaces Poll
例如 Primefaces 投票
<h:form id="form">
<h:outputText id="txt_count" value="#{counterBean.count}" />
<p:poll interval="3" listener="#{counterBean.increment}" update="txt_count" />
</h:form>
Link to showcase Primefaces Ajax Poll
Pure JSF approach would be to use js timer that will invoke periodical document.getElementById('someFormId:idOfButton').click();
纯 JSF 方法是使用 js 计时器来调用期刊 document.getElementById('someFormId:idOfButton').click();
or jquery $("#someFormId\\:idOfButton").click();
或 jquery $("#someFormId\\:idOfButton").click();
while the button will look like this
而按钮看起来像这样
<h:commandButton id="idOfButton">
<f:ajax render="txt_count"></f:ajax>
</h:commandButton>
something like this
像这样的东西
setInterval(function(){$("idOfButton").click()},3000);
More about timer JavaScript Timing Events
更多关于计时器JavaScript 计时事件
回答by simgineer
In RichFaces there is a poll component as well. Here is a nice example theyprovide
RichFaces 中也有一个投票组件。这是他们提供的一个很好的例子
<a4j:region>
<h:form>
<a4j:poll id="poll" interval="1000" enabled="#{userBean.pollEnabled}" reRender="poll,grid"/>
</h:form>
</a4j:region>
<h:form>
<h:panelGrid columns="2" width="80%" id="grid">
<h:panelGrid columns="1">
<h:outputText value="Polling Inactive" rendered="#{not userBean.pollEnabled}" />
<h:outputText value="Polling Active" rendered="#{userBean.pollEnabled}" />
<a4j:commandButton style="width:120px" id="control" value="#{userBean.pollEnabled?'Stop':'Start'} Polling" reRender="poll, grid">
<a4j:actionparam name="polling" value="#{!userBean.pollEnabled}" assignTo="#{userBean.pollEnabled}"/>
</a4j:commandButton>
</h:panelGrid>
<h:outputText id="serverDate" style="font-size:16px" value="Server Date: #{userBean.date}"/>
</h:panelGrid>
</h:form>

