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:
But if I add <h:message for="loginForm"></h:message>
into my code, the output will be:
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?