当h:commandButton被调用时显示h:消息

时间:2015-05-04 21:55:14

标签: jsf

我在jsf:

中有一个password recovery表单
<h:form id="arForm">
    <p:panel id="arp">
        <p:focus context="arp"/>
        <h:panelGrid columns="2" columnClasses="...">

            <h:message id="emailMsg" for="email"/>
            <h:inputText id="email" value="#{accountBean.email}" required="true" label="email">
                <f:validateRegex pattern="..."/>
                <f:ajax event="blur" render="emailMsg"/>
            </h:inputText>
            <p:watermark for="email" value="Enter email address..."/>

            <h:commandButton id="recoverBtn" value="Recovery" action="#{accountBean.sendEmail()}" class="recoverBtn">
                <f:ajax execute="@form" render="@form"/>
            </h:commandButton>
        </h:panelGrid>
        <h:messages id="emailResult" globalOnly="true" showDetail="true"/>
    </p:panel>
</h:form>

这是accountBean.sendEmail()

public void sendEmail() throws Exception {
    boolean sendResult = false;
    FacesContext.getCurrentInstance().addMessage("arForm:emailMsg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Please Wait... "));

    sendResult = sendHtmlEmail(...to entered email);

    if (sendResult) {
         // emailResult message that says there is an internal problem
    } else {
        // emailResult message that says there is an internal problem
    }
}

我希望当用户输入电子邮件并点击Recovery按钮时,会显示Please Wait...消息,直到发送电子邮件并显示相应的消息。

但问题是Please Wait...消息根本没有显示。

1 个答案:

答案 0 :(得分:1)

与您的预期相反,视图不会被渲染,直到action方法返回。

您需要在DOM click事件期间在客户端显示该消息。 JavaScript对此很有帮助。

基本上:

<h:form>
    <span id="pleaseWait" style="display:none">Please wait ...</span>
    ...
    <h:commandButton ... onclick="$('#pleaseWait').show()">
        <f:ajax ... render="@form" />
    </h:commandButton>
</h:form>

(显然内联CSS / JS在实际代码中重构)