Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF FacesContext#addMessage is not displayed

Tags:

jsf

primefaces

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.

like image 347
perissf Avatar asked May 01 '12 10:05

perissf


1 Answers

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(...

like image 68
Daniel Avatar answered Nov 15 '22 02:11

Daniel