终止命令按钮actionListener的长时间ajax调用?

时间:2022-10-07 16:08:00

I would like to be able to stop the execution of a lengthy task started with a commandButton:

我希望能够停止执行一个命令按钮开始的冗长任务:

<p:commandButton id="startBatch"value="Go" actionListener="#{batchController.sendBatch()}"ajax="true"  />

So in my Model I added a boolean "abort" and inside sendBatch() I added a check to stop the loop when the boolean becomes true:

因此,在我的模型中,我添加了一个布尔“abort”,在sendBatch()中,我添加了一个检查,以在布尔值为真时停止循环:

for (int i = 1; i <= batch.size(); i++) {
    if (batchModel.isAbort()) {
        break;
    }
}

And in my XHTML I added a new button that calls a method that sets this boolean to true:

在我的XHTML中,我添加了一个新按钮,该按钮调用一个方法,该方法将布尔值设置为true:

<p:commandButton id="abortBatchButton"
value="Abort"
actionListener="#{batchController.abort()}"
ajax="true"/>   

The problem is that abort() is called after the sendBatch() method is done.

问题是在sendBatch()方法完成之后调用abort()。

Is there a simple way to implement this ?

有没有一种简单的方法来实现它?

1 个解决方案

#1


2  

By default p:commandButtons use Ajax. However, multiple actions (requests) are queued and executed synchronically by default. So, if you have started a lengthy action, the next action will be processed when the previous actions are finished.

默认情况下,命令按钮使用Ajax。但是,默认情况下,多个操作(请求)将被排队并同步执行。因此,如果您已经启动了一个冗长的操作,那么当前面的操作完成时,将处理下一个操作。

If you don't want to queue actions, you should use async="true" on the respective buttons.

如果您不想对操作进行排队,您应该在各自的按钮上使用async="true"。

In your case:

在你的例子:

<p:commandButton id="startBatch"
                 value="Go"
                 async="true"
                 actionListener="#{batchController.sendBatch()}" />

I've removed ajax="true", since it's default.

我已经删除了ajax="true",因为它是默认的。

#1


2  

By default p:commandButtons use Ajax. However, multiple actions (requests) are queued and executed synchronically by default. So, if you have started a lengthy action, the next action will be processed when the previous actions are finished.

默认情况下,命令按钮使用Ajax。但是,默认情况下,多个操作(请求)将被排队并同步执行。因此,如果您已经启动了一个冗长的操作,那么当前面的操作完成时,将处理下一个操作。

If you don't want to queue actions, you should use async="true" on the respective buttons.

如果您不想对操作进行排队,您应该在各自的按钮上使用async="true"。

In your case:

在你的例子:

<p:commandButton id="startBatch"
                 value="Go"
                 async="true"
                 actionListener="#{batchController.sendBatch()}" />

I've removed ajax="true", since it's default.

我已经删除了ajax="true",因为它是默认的。