Newbie
Newbie

Reputation: 1714

JSF FacesMessage shows detail message instead of summary message

I am not clear on how addMessage() and FacesMessage works. If I didnt specify a <h:message> for it to display a message, the summary message will be displayed. But if I create a <h:message> field for it, the detailed message will be displayed. Why? How to select which message to display? Below is my code:

Bean

public String login()
{
    if(password.equals("123123"))
    {
        session.setAttribute("username", username);
        return "home";
    }
    else
    {
        FacesContext.getCurrentInstance().addMessage("loginForm", new FacesMessage(FacesMessage.SEVERITY_WARN,"Incorrect Username and Passowrd", "Please enter correct username and Password"));
        return "login";
    }
}

JSF

<h:form id="loginForm">
    <h:outputLabel for="username" value="Username: " />
    <h:inputText id="username" value="#{loginBean.username}" required="true" requiredMessage="Username is required" />
    <h:message for="username"></h:message>
    <br /><br />

    <h:outputLabel for="password" value="Password: " />
    <h:inputSecret id="password" value="#{loginBean.password}"></h:inputSecret>
    <h:message for="password"></h:message>
    <br /><br />

    <h:commandButton action="#{loginBean.login()}" value="Login"></h:commandButton>
    <br /><br />
</h:form>

The code above will produce the following output:
enter image description here

But if I add <h:message for="loginForm"></h:message> into my code, the output will be: enter image description here

It shows the detailed message instead of summary message, it is not a big deal actually but I just want to know why this happens? Can anyone explain?

Upvotes: 1

Views: 2002

Answers (1)

BalusC
BalusC

Reputation: 1108632

That orange message will only appear when you have javax.faces.PROJECT_STAGE set to Development and you forgot to declare a <h:messages> in the view. In such case JSF can't show the message anywhere in the page. Normally, this is only logged as a warning in server log like so:

There are some unhandled FacesMessages, this means not every FacesMessage had a chance to be rendered

However, for developer's convenience this is also appended to the end of the webpage when project stage is set to development. But you should not rely on that as normal functionality of the webapp. You should take that as a hint that you overlooked something or did something wrong. The correct solution would be to add an appropriate <h:message for="xxx"> or at least a <h:messages> as fallback.

That the development stage message shows the summary instead of the detail is your least concern. It should in first place not have been displayed at all.

See also:

Upvotes: 2

Related Questions