【问题标题】:JSF, refresh periodically a component with ajax?JSF,用ajax定期刷新一个组件?
【发布时间】:2012-06-29 00:03:38
【问题描述】:

我正在开发一个应用程序 JSF,我想定期刷新一个带有 ajax 的组件,例如 facebook 通知区域行为。 我该怎么做?

【问题讨论】:

  • 在未来的问题中,如果您明确提及您都使用的组件库,例如 PrimeFaces,将会很有帮助。如果您不提及任何内容,则始终假定为标准 JSF。

标签: ajax jsf notifications refresh


【解决方案1】:

民意调查是你需要使用的

轮询组件在指定的时间间隔内进行 ajax 调用。

例如 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>

展示链接Primefaces Ajax Poll

纯粹的 JSF 方法是使用 js 定时器来调用周期性的document.getElementById('someFormId:idOfButton').click();

或 jquery $("#someFormId\\:idOfButton").click();

虽然按钮看起来像这样

<h:commandButton id="idOfButton">
    <f:ajax render="txt_count"></f:ajax>
</h:commandButton>

类似的东西

setInterval(function(){$("idOfButton").click()},3000);

更多关于定时器JavaScript Timing Events

【讨论】:

  • 根据 OP 的问题历史,他正在使用 PrimeFaces,所以这是一个完美的套装。
【解决方案2】:

在 RichFaces 中也有一个投票组件。这是一个很好的例子they提供

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2013-01-11
    • 2011-10-07
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多