JSF 2 Message and Messages Example

The <h:message/> and <h:messages/> can be used for displaying the error, fatal error, warning and information messages for single component and for the whole current view respectively. When it comes to develop a JSF page, it happens for a component/view to have a messages that tell the user about some missing or wrong data must be provided/fixed to ensure that the business logic for your managed beans can execute smoothly and without any problems.

The message/messages component provides the capability for the developer to provide the desired style for the different kind of messages, so the styles for different messages can provide a deep identification in which the user can discriminate between those that are just providing an information from those that couldn’t proceed any further if they aren’t fixed.

The severity level which passed through adding a faces message into component/view determine the type of the messages whether they are error or even just an information. Any message/messages contains either type of information; detail and summary, where the developer has controlled if it does wish to render the detail alone or the summary alone or it has the both. One remaining important issue, in that when it comes to use the message/messages, the bundled messages are brought for making a localization of component/view messages.

Also Read:

This tutorial will explain the proper way for using the message and messages for displaying the messages for a specific component or for the current view.

1. Managed Bean


package net.javabeat.jsf;

import java.util.Locale;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

public class IndexBean {

	private String locale = "en";
	private String message = "";
	private String email = "";

	public String getLocale() {
		return locale;

	public void setLocale(String locale) {
		this.locale = locale;

	public String getMessage() {
		return message;

	public void setMessage(String message) {
		this.message = message;

	public String getEmail() {
		return email;

	public void setEmail(String email) {
		this.email = email;

	public String changeLocale(String locale){
		// Change the locale attribute
		this.locale = locale;
		// Change the locale of the view
		FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(this.locale));
		return "";

	public String send(){
			// Bring the error message using the Faces Context
			String errorMessage = FacesContext.getCurrentInstance().getApplication().
			// Add View Faces Message
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
			errorMessage, errorMessage);
			// Add the message into context for a specific component
			FacesContext.getCurrentInstance().addMessage("form:message", message);

			// Bring the error message using the Faces Context
			String errorMessage = FacesContext.getCurrentInstance().getApplication().
			// Add View Faces Message
			// Cause the application doesn't functional if you've got missing the email
			// The severity case is Error or fatal error
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,errorMessage,
			// Add the message into context for a specific component
			FacesContext.getCurrentInstance().addMessage("form:email", message);
			// Return empty token for navigation handler
			return "";

		// Return confirmation message that the message has been sent

		// Bring the information message using the Faces Context
		String confirmMessage = FacesContext.getCurrentInstance().getApplication().
		// Add View Faces Message
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,confirmMessage, confirmMessage);
		// The component id is null, so this message is considered as a view message
		FacesContext.getCurrentInstance().addMessage(null, message);
		// Return empty token for navigation handler
		return "";

2. The View


<html xmlns="http://www.w3.org/1999/xhtml"
	<title><h:outputText value="#{msg.title}"/></title>
		.err {
			color: red;
		.warn {
			color: yellow;
		.info {
			color: blue;
	<h:form id="form">
	<div style="border: 1">
		<span> Global Message Area:</span>
		<h:messages globalOnly="true" errorClass="err" warnClass="warn" infoClass="info"></h:messages>
		<h:messages globalOnly="false" errorClass="err" warnClass="warn" infoClass="info"></h:messages>
	<f:loadBundle var="messages" basename="net.javabeat.jsf.messages"></f:loadBundle>
			<h:outputText value="#{msg.head}" />
			<h:outputText value="#{msg.example_name}" />
		<h:outputText value="#{msg.hello_message}"/>
		<h:outputText value="#{messages.inlineLoad}"/>
		<h:panelGrid columns="3">
			<h:outputText value="#{msg.enter_message}"/>
			<h:inputText id="message" value="#{indexBean.message}"/>
			<h:message for="message" errorClass="err" warnClass="warn" infoClass="info"></h:message>
			<h:outputText value="#{msg.enter_email}"/>
			<h:inputText id="email" value="#{indexBean.email}"/>
			<h:message for="email" errorClass="err" warnClass="warn" infoClass="info"></h:message>
		<h:commandButton value="Send" action="#{indexBean.send}"/>
		<h:commandButton value="#{messages.german}"
				rendered="#{indexBean.locale == 'en'}"/>
		<h:commandButton value="#{messages.english}"
				rendered="#{indexBean.locale == 'de'}"/>

3. faces-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"

4.The Directory Structure

JSF 2 Message Example 1

5. JSF 2 Messages Demo

The below snapshots will show you a complete scenario of using the messages either message component for component-based messages or messages for view-based messages.
The developed scenario has supported the locales (English/German) in order to explain the concept of localizing the messages that consumed by. The suggested scenario contains the following points that should bring your attention:

  • The message component used for displayed the error/info/warn messages for a specific component in the view.
  • The messages component used for displayed the error/info/warn messages for the whole view.
  • The messages component provide attribute (globalOnly) that used for determine whether the messages that being added in the faces context for specific component will be displayed or not.
  • The styles err, warn and info are used by the jsf message/messages component based on the used severity.

JSF 2 Message Example 2

JSF 2 Message Example 3

JSF 2 Message Example 4

JSF 2 Message Example 5

JSF 2 Message Example 6

JSF 2 Message Example 7

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>