首页 > 解决方案 > 当 p:poll 停止时传递对话 id 作为参数

问题描述

我正在使用 Primefaces 和 p:poll,因为我想在条件满足后轮询停止时导航到另一个页面。在两个页面上都使用了相同的对话 bean。(实际上有三个页面使用了这个bean)。

但是我不知道如何在轮询停止时将对话 ID 作为参数传递,如果有链接或按钮则传递的方式,因为 f:param 不能与 p:poll 一起使用。

任何帮助,将不胜感激。

提前致谢。

标签: javajsfprimefaces

解决方案


我认为你有两个问题:

  1. 如何制作多页向导?
  2. 如何检查任务(搜索)是否完成?

如何制作多页向导?

我认为这不是您的主要问题,您已经找到了解决方案。这只是为了完整性。

您可以使用流程对话(我会使用它)。


如何检查任务(搜索)是否完成?

为此,您还获得了与此类似的解决方案。

但正如@Jasper_de_Vries 在评论中所说,websocket的性能比p:poll.


所以这是我对第二个问题的解决方案:

演示 XHTML 文件:

<h:form>
    <!-- must be in form when it has nested f:ajax's -->
    <f:websocket channel="demo" scope="view">

        <!-- renders the form and the 'someId' component -->
        <!-- when receives 'complete' message -->
        <f:ajax event="complete" render="@form :someId" />
    </f:websocket>    

    <!-- display result here -->
</h:form>

<xy:whatever id="someId">
    <!-- display result here -->
</xy:whatever>

还有你的豆子:

@Named
@ConversationScoped
public class Demo {

    @Inject
    private SomeService service;

    @Inject @Push
    private PushContext demo; // variable name must match the channel name

    private Result result; // getter + setter

    // conversation utilities, etc.
    
    private void sendMessage() {
        demo.send("complete"); // this is the whole magic
    }

    public void startLongTask() {
        service.startLongTask(/* parameters */, result -> {
            // this runs when the callback is accepted
            this.result = result;
            sendMessage();
        });
    }
}

一些服务:

@Stateless/@Stateful
public class SomeServiceService {

    @Asynchronous
    public void startLongTask(/* parameters*/, Consumer<Result> callback) {
        // very long task ...

        callback.accept(result);
    }
}

基本上,当用户单击按钮时,就会启动一个长任务(例如搜索)。当服务完成时,它将调用回调并更新 UI。

f:websocket是 JSF 2.3 的一个特性。如果您不使用 JSF 2.3,请查看 Omnifaces Websocket o:websocket


推荐阅读