【问题标题】:JSF FacesContext#addMessage is not displayedJSF FacesContext#addMessage 不显示
【发布时间】:2012-05-01 10:40:16
【问题描述】:

在我的previous question 中,我遇到了从登录表单显示验证消息的问题。该问题现已解决,但这次我无法使用FacesContex#addMessage 显示自定义消息。

使用 JSF + PrimeFaces。

<p:dialog header="Login" widgetVar="loginDlg">
    <h:form id="loginForm">
        <h:panelGrid columns="3" cellpadding="5">
            <h:outputLabel for="username" value="Username:" />
            <p:inputText value="#{loginBean.username}" id="username" required="true" label="username" />
            <p:message for="username" />
            <h:outputLabel for="password" value="Password:" />
            <h:inputSecret value="#{loginBean.password}" id="password" required="true" label="password" />
            <p:message for="password" />
            <f:facet name="footer">
                <p:commandButton value="Login" id="loginDlgButton" update=":loginForm,:welcomeMsg" actionListener="#{loginBean.login}"
                                    oncomplete="handleLoginRequest(xhr, status, args)"/>
                <p:message for="loginDlgButton" />
            </f:facet>
        </h:panelGrid>
    </h:form>
</p:dialog>

LoginBean中(SessionScopedManagedBean):

public void login() {
    FacesContext context = FacesContext.getCurrentInstance(); 
    RequestContext rContext = RequestContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest(); 
    try { 
        request.login(this.username, this.password); 
        rContext.addCallbackParam("loggedIn", true);
    } catch (ServletException e) { 
        rContext.addCallbackParam("loggedIn", false);
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials")); 
    } 
}

当验证成功并且登录失败时,此代码应显示“无效凭据”消息,但不会。此外,在我的网页正文的某处,我还添加了这一行:

<p:messages autoUpdate="true" />

但我的消息甚至没有显示在那里。

Javadocs 这么说

如果 clientId 为 null,则假定此 FacesMessage 不与任何特定组件实例关联

但我不明白这是什么意思。

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    &lt;p:messages autoUpdate="true" /&gt; 放置在您的 form 中或由您的 commandButton 的 update 更新的某个包装器中,或者将 loginDlgButton 放置在 context.addMessage(... 中而不是 null

    【讨论】:

    • 谢谢丹尼尔。请参阅我对其他答案的评论。
    【解决方案2】:

    我在您的代码中没有看到 p:messages 标记。它与 p:message 标记不同。 p:message 附加到另一个组件并显示为验证的一部分。 p:messages(或 p:growl)组件是您在 bean 中更新的内容。尝试像这样添加消息或咆哮组件:

    <h:form id="loginForm">
    <p:growl id="messageGrowl" showDetail="true" sticky="false" />
    <h:panelGrid columns="3" cellpadding="5">
    

    【讨论】:

    • 我的错。它已经在工作,但答案有点晚了和/或我没看到!因此,消息被 p:messages 标签捕获。但是,如何在不将 componentId 放入 bean 的情况下使用非全局 p:message 标签捕获消息?
    • 我想你是在问如何捕捉消息并显示在对话框上?在 p:dialog 上放一个 p:messages。然后,您可以将其添加到命令按钮上的更新列表中。如果在上下文中发回一条消息,它将更新该消息。您可能需要关闭其他消息组件上的 autoUpdate="true" 或两者都将被更新。
    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2011-09-01
    • 2011-08-06
    • 2011-06-01
    相关资源
    最近更新 更多