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

问题描述 投票:0回答:1

我正在尝试实现一个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>

UserInputMB:

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;
    }
}

在此示例中,当按下按钮时,pressButton方法应使对话框可视化并等待用户输入,然后继续执行。

我还在stackoverflow上发现了类似的问题:Synchronous dialog invocation from managed bean

但是我的情况大不相同。我被迫实施这种行为。预先感谢!

asynchronous jsf primefaces
1个回答
2
投票

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

JSF:

<!-- 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" />

Bean:

@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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.