3

考虑 JSF 页面中的以下形式:

<h:form id="countForm">
  <h:outputText value="#{myBean.count}" />
  <h:commandButton value="Increment the count!">
    <f:ajax listener="#{myBean.increment()}" render="@form" />
  </h:commandButton>
</h:form>

每当用户通过 ajax 请求单击按钮时,这个简单的代码都会增加计数器。

假设我还想在表单更新(重新呈现)时向用户显示通知计数器的新值的警报。我该怎么做?有没有类似下面的代码?

<h:form onUpdate="alert('the new value is #{myBean.count()}')">

我知道我可以使用 primefaces 的“oncomplete”方法,但事件将链接到按钮而不是表单,这不是我想要的。

计数器不是我想要做的,这只是一个简单的例子来更好地展示我的问题。

预先感谢您的任何帮助。

4

4 回答 4

2

由于您的确切要求不清楚,因此很难提供准确的答案。
1. 你试过<a4j:form>which has onCompleteevent吗?没有解决你的问题吗?2. 如果您在重新渲染
表单后绝对需要运行脚本,您可以在表单内放置标签,并将 javascript 代码放入其中,如下所示。<script>

<h:form id="frm">
  <script>
    alert('#{myBean.count()}');
  </script>
</h:form>

每次呈现表单时都会显示此警报。
请注意,如果您需要阻止此 JS 在页面加载时运行,您可能需要添加如下条件。

<head>
  <script>
    var showAlert = false;
  </script>
</head> 
<body>
<h:form id="frm">
  <script>
    if(showAlert) {
      alert('#{myBean.count()}');
    }
    showAlert = true;
  </script>
</h:form>
</body>
于 2012-11-15T04:04:03.833 回答
1

您可以使用PrimeFaces中的 ajax 状态标记组件:<p:ajaxStatus>RichFaces:(<a4j:status>链接到其文档)。两个组件都处理表单中进行的 ajax 调用的开始和完成事件。

我没有在 JSF 2 应用程序中尝试过任何这些,但我建议使用 PrimeFaces (至少文档比 RichFaces 更好)。我<a4j:status>在 JSF 1.2 + RichFaces 3.3 应用程序中使用 RichFaces 向用户显示/隐藏任何 ajax 请求的对话框。它看起来像这样(并且可以给你一个想法):

<a4j:status onstart="Richfaces.showModalPanel('mdpWaitMessage')" 
    onstop="Richfaces.hideModalPanel('mdpWaitMessage')" />
<!-- defining the modal (popup) panel to show the user there was an ajax action -->
<rich:modalPanel id="mdpWaitMessage" resizeable="false" zindex="5000"
    minHeight="30" minWidth="10" height="70" width="150">
    <f:facet name="header">
        <h:outputText value="#{aplicacion.waitMessageTitle}" />
    </f:facet>
    <h:graphicImage value="#{aplicacion.waitMessagePath}" />
    <h:outputText value="#{aplicacion.waitMessageDescription}" />
</rich:modalPanel>
于 2012-11-15T06:31:57.270 回答
0

看一眼javax.faces.event.PreRenderComponentEvent.

<h:form id="myForm">
  <f:event type="preRenderComponent"
           listener="#{someActionListener}" />
</h:form>

每次(重新)呈现表单时,都会首先调用 actionListener。

于 2012-11-14T22:23:47.700 回答
0

有一种方法不是很优雅,但我在没有其他选择的情况下使用它。我正在使用<h:panelGroup>包含一个块的 a,并且我在部件的一部分中<script>使用了 panelGroup idrender=<f:ajax

<h:form id="countForm">
  <h:outputText value="#{myBean.count}" />
  <h:commandButton value="Increment the count!">
    <f:ajax listener="#{myBean.increment()}" render="postAjax" />
  </h:commandButton>
  <h:panelGroup id="postAjax">
    <script>
     //...
    </script>
  </h:panelGroup>
</h:form>

panelGroup请注意,如果位于 aui:repeat或中,这将无法正常工作datatable

于 2012-11-15T11:40:56.930 回答