Asynchronous 如何使托管bean等待用户输入-JSF

Asynchronous 如何使托管bean等待用户输入-JSF,asynchronous,jsf,primefaces,Asynchronous,Jsf,Primefaces,我试图实现一个JSF页面,用户应该在其中插入一些数据。特别是,当按下按钮时,会出现一个对话框,要求用户输入。主要的问题是,应该停止支持bean的执行,等待用户响应 下面是一个玩具示例 JSF页面: <h:form id="label"> <p:dialog header="User input" widgetVar="dlg2" visible="true" modal="false"

我试图实现一个JSF页面,用户应该在其中插入一些数据。特别是,当按下按钮时,会出现一个对话框,要求用户输入。主要的问题是,应该停止支持bean的执行,等待用户响应

下面是一个玩具示例

JSF页面:

        <h:form id="label">
            <p:dialog header="User input" widgetVar="dlg2"
                visible="true" modal="false"
                resizable="false" height="100" width="300">
                <br />
                <h:inputText value="#{userInputMB.userInput}"></h:inputText>
            </p:dialog>
            <p:commandButton action="#{userInputMB.pressButton}"></p:commandButton>
        </h:form>
在本例中,按下按钮时,pressButton方法应显示对话框并等待用户输入,然后继续执行

我在stackoverflow上也发现了类似的问题:

但我的情况完全不同。我被迫实施这种行为。
提前谢谢

以下示例包含一个对话框和一个按钮。按钮准备输入并打开对话框。在对话框中,第二个按钮调用操作以处理输入

JSF:


以下示例包含一个对话框和一个按钮。按钮准备输入并打开对话框。在对话框中,第二个按钮调用操作以处理输入

JSF:


如果您只是想要一个类似确认的对话框,可以使用PrimeFaces确认对话框:否则创建您自己的对话框,从commandButton中删除该操作并仅显示您需要的对话框。在对话框中添加一个新按钮,该按钮调用支持bean中的操作。你也可以在你的inputText上添加一个调用动作的监听器,而不是一个新按钮。我将把它分为两种方法——一种用于显示问题,另一种用于处理用户输入。由于您的bean已经进行了会话处理,您可以在bean的第二个操作中存储所需的任何数据。对不起,我不明白-您所说的“停止执行”是什么意思?如果您在操作中没有做更多的事情,那么准备userInput将不会执行更多的操作。对不起,JSF/Web不是这样工作的。您需要两个HTTP请求(一个用于预处理,一个用于输入),您不能在服务器端阻止以等待另一个请求进入。@TomStroemer是正确的。。。这不是JSF特有的限制,而且很可能stackoverflow中已经存在此Q的“副本”。如果您只需要类似确认的对话框,可以使用PrimeFaces确认对话框:否则,创建自己的对话框,从commandButton中删除该操作,只显示所需的对话框。在对话框中添加一个新按钮,该按钮调用支持bean中的操作。你也可以在你的inputText上添加一个调用动作的监听器,而不是一个新按钮。我将把它分为两种方法——一种用于显示问题,另一种用于处理用户输入。由于您的bean已经进行了会话处理,您可以在bean的第二个操作中存储所需的任何数据。对不起,我不明白-您所说的“停止执行”是什么意思?如果您在操作中没有做更多的事情,那么准备userInput将不会执行更多的操作。对不起,JSF/Web不是这样工作的。您需要两个HTTP请求(一个用于预处理,一个用于输入),您不能在服务器端阻止以等待另一个请求进入。@TomStroemer是正确的。。。这不是JSF特有的限制,而且在stackoverflow中很可能已经有了这个Q的“副本”。
package jsfpackage;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class UserInputMB {
    private String userInput;
    private boolean visualizeDialog = false;

    public UserInputMB() {

    }

    public void pressButton() {
        System.out.println("Executing the pressButton method..");
        //here I need to visualize the dialog and wait for the user input
        System.out.println(userInput);
    }
    public String getUserInput() {
        return userInput;
    }

    public void setUserInput(String userInput) {
        this.userInput = userInput;
    }

    public boolean isVisualizeDialog() {
        return visualizeDialog;
    }

    public void setVisualizeDialog(boolean visualizeDialog) {
        this.visualizeDialog = visualizeDialog;
    }
}
<!-- dialog for input -->
<p:dialog id="inputDialog" widgetVar="inputDialog" header="Input here">
    <p:inputText value="#{userInputMB.userInput}" />
    <p:commandButton action="#{userInputMB.processInput}" />
</p:dialog>

<!-- Calls the action to prepare the input and updates and opens the dialog -->
<p:commandButton value="show dialog" action="#{userInputMB.prepareInput}"
                 oncomplete="PF('inputDialog').show()" process="@this" update=":inputDialog" />
@ManagedBean
@SessionScoped
public class UserInputMB {

    private String userInput;

    public void prepareInput() {
        userInput = "Please enter your input here";
    }

    public void processInput() {
        if("inputYouWanted".equals(userInput)) {
            System.out.println("hurray, correct input!");
        }
    }

    public String getUserInput() {
        return userInput;
    }

    public void setUserInput(String userInput) {
        this.userInput = userInput;
    }
}