perissf
perissf

Reputation: 16273

JSF FacesContext#addMessage is not displayed

In my previous question I had the problem of displaying validation messages from a Login form. That issue is now solved, but this time I am not able to display a custom message with FacesContex#addMessage.

Using 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>

In LoginBean (a SessionScoped ManagedBean):

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

This code, when validation succeeds and login fails, should display the "Invalid credential" message, but doesn't. Moreover, somewhere in the body of my web page, I have also added this line:

<p:messages autoUpdate="true" />

but my message isn't displayed even there.

Javadocs say that

If clientId is null, this FacesMessage is assumed to not be associated with any specific component instance

But I can't understand what this means.

Upvotes: 5

Views: 19605

Answers (2)

Daniel
Daniel

Reputation: 37051

place <p:messages autoUpdate="true" /> inside your form or inside some wrapper that is being updated by update of your commandButton , or place loginDlgButton instead of null in context.addMessage(...

Upvotes: 5

SteveS
SteveS

Reputation: 1030

I don't see a p:messages tag in your code. It is not the same as the p:message tag. p:message is attached to another component and is displayed as part of validation. The p:messages (or p:growl) component is what you are updating in your bean. Try adding a messages or growl component like this:

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

Upvotes: 4

Related Questions