JSF <h:message> Tag

It is used to display a single message for a particular component. You can display your custom message by passing id of that component into the for attribute.

# Tag Attributes

Attribute Description
for It is mandatory tag which is used to assign component's id, for that message is composed.
errorClass It is used to apply CSS style class to any message with a severity class of "ERROR".
errorStyle It is used to apply CSS style to any message with a severity class of "ERROR".
fatalClass It is used to apply CSS style class to any message with a severity class of "FATAL".
FatalStyle It is used to apply CSS style to any message with a severity class of "FATAL".
infoClass It is used to apply CSS style class to any message with a severity class of "INFO".
InfoStyle It is used to apply CSS style to any message with a severity class of "INFO".
tooltip It is used to display detail portion of the message as a tooltip.
warnClass It is used to apply CSS style class to any message with a severity class of "WARN".
warnStyle It is used to apply CSS style to any message with a severity class of "WARN".

# Example

// index.xhtml


<h:form id="form">
<h:outputLabel for="username">User Name</h:outputLabel>
<h:inputText id="name-id" value="#{user.name}"/>
<h:message for="name-id" style="color: red"/>
<br/>
<h:outputLabel for="mobile">Mobile No.</h:outputLabel>
<h:inputText id="mobile-id" value="#{user.mobile}"/>
<h:message for="mobile-id" style="color: red"/>
<br/>
<h:commandButton value="OK" action="response.xhtml"></h:commandButton>
</h:form>

// User.java


import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@ManagedBean
@RequestScoped
public class User{
@NotNull(message = "Name can't be empty")
String name;
@NotNull(message = "Mobile can't be empty")
@Size(min = 10, max = 10, message = "Mobile must have 10 digits")
String mobile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}

Output:

JSF H message tag 1