当 p:poll 停止时将对话 ID 作为参数传递

Pass conversation id as parameter when p:poll stops

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

但我不知道如何在轮询停止时将对话 ID 作为参数传递,如果有 link 或按钮,将采用这种方式传递,因为无法使用 f:param p:poll.

如有任何帮助,我们将不胜感激。

提前致谢。

我认为你有两个问题:

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

如何制作多页向导?

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

You can either use a or a conversation (I would use this).


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

为此,您还得到了类似于 this 的解决方案。

但是正如@Jasper_de_Vries 在评论中所说,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.