【问题标题】:Primefaces messages in a dialog is not displayed不显示对话框中的 Primefaces 消息
【发布时间】:2014-08-15 11:06:47
【问题描述】:

我正在使用 Primefaces,并且有一个登录对话框:

<p:dialog widgetVar="dlgLogin" header="Login" modal="true">
        <p:outputPanel>
            <h:form>
                <p:messages for="msgLogin" autoUpdate="true" closable="true" showSummary="true" showDetail="false" severity="error"/>
                <p:panel>
                    <h:panelGrid columns="2">
                        <p:outputLabel value="User Name"/>
                        <p:inputText id="userName" value="#{loginBean.userName}"
                                     required="true" requiredMessage="User Name is required"/>

                        <p:outputLabel value="Password"/>
                        <p:password id="password" value="#{loginBean.password}"
                                    required="true" requiredMessage="Password is required"/>
                    </h:panelGrid>
                </p:panel>
                <p:commandButton icon="ui-icon-arrowrefresh-1-n" value="Cancel" onclick="PF('dlgLogin').hide()"/>
                <p:commandButton type="submit" value="Login" 
                                 ajax="false" validateClient="true"
                                 oncomplete="handleClose(xhr, status, args);"
                                 action="#{loginBean.login}"/>
            </h:form>
        </p:outputPanel>
    </p:dialog>

提交登录是故意非ajax,所以整个页面都会刷新。

服务器代码是:

FacesContext.getCurrentInstance().addMessage("msgLogin", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Wrong User Name or Password.", "Wrong User Name or Password."));

如果在服务器上登录失败,我想显示一条消息,并让对话框保持打开状态。但它不起作用。

如何做到这一点?

谢谢。

【问题讨论】:

  • 你真的需要ajax="false"吗?如果登录成功,还有其他方法可以刷新页面,并且它作为 ajax 请求工作得很好。 :)

标签: jsf primefaces


【解决方案1】:

不要在 JSF 中而是在 Java 中关闭对话框:

if (successfulLogin) {
    RequestContext.execute("PF('dlgLogin').hide()");
} else {
    FacesContext.getCurrentInstance().addMessage("msgLogin", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Wrong User Name or Password.", "Wrong User Name or Password."));
}

【讨论】:

  • 我有 2 个问题: * 消息未显示在对话框中。 * 我没有关闭对话框,而是刷新了整个页面,因为它不是 ajax。
  • "如果服务器登录失败,我想显示一条消息,并让对话框保持打开状态。但它不起作用。" - 所以默认不关闭对话框是解决问题的第一步,不是吗?
  • 尝试将此 update="@form" 添加到您的 Login 按钮,或者如果它不起作用,请尝试在你的表格
  • 也许我的问题不清楚:我有 2 个案例:案例 1 - 如果登录成功 --> 整个页面应该更新。 (update="@all" 在更新对话框时遇到问题,因此我使用 ajax="false")。情况 2 - 如果登录失败 --> 页面不应该提交,对话框应该保持打开状态,并且应该显示错误消息。
  • 好的,我解决了这个问题。我删除了 ajax="false",如果登录成功,托管 bean 代码会重新加载页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多