Main Tutorials

JSF 2 message and messages example

In JSF, you can output message via following two messages tags :

  1. h:message – Output a single message for a specific component.
  2. h:messages – Output all messages in current page.

See following JSF 2.0 example to show the use of both “h:message” and “h:messages” tags to display the validation error message.

JSF page…


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >
     
    <h:body>
    	
    	<h1>JSF 2 message + messages example</h1>
		
	<h:form>
			
	  <h:messages style="color:red;margin:8px;" />
			
	  <br />
			
	  <h:panelGrid columns="3">
				
		Enter your username :
				
		<h:inputText id="username" value="#{user.username}" 
			size="20" required="true"
			label="UserName" >
			<f:validateLength minimum="5" maximum="10" />
		</h:inputText>
 
		<h:message for="username" style="color:red" />

		Enter your age :
		<h:inputText id="age" value="#{user.age}" 
			size="20" required="true"
			label="Age" >
			<f:validateLongRange for="age" minimum="1" maximum="200" />
		</h:inputText>
 
		<h:message for="age" style="color:red" />
				
	  </h:panelGrid>
		
	  <h:commandButton value="Submit" action="result" />
		
      </h:form>
		
    </h:body>
</html>

Here’s the output…

jsf2-message-example

Download Source Code

Download It – JSF-2-Message-Example.zip (10KB)

Reference

  1. JSF h:messages JavaDoc
  2. JSF h:message JavaDoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Mufas
3 years ago

Hi, How can I add one css class for this summary messages. Right now Inside summary class we have the list. Each and every error message I need one class. Is that possible ?

Tomasz G?owacki
9 years ago

I would expand this example to show how to add message in bean, because it is not as intuitive as people could think. Also you can expand this example to show globalOnly attribute in . then example would be complete and awesome 😉

Vincent
9 years ago

This saved my life at work! Couldn’t understand where all these #{msgs} were being defined!

rekiem87
11 years ago

Thanks for your posts, very friendly and understable, but i have a question, how do you work when you need an extra message for an especific error?, for example, if you have the login page, with a message for the username field and other for the password, then, when the user write the wrong password you need an extra message to show “Login/password invalid”, but if you do it with messages the other two will show up when you don’t need it, and you can’t use a message since that error doesn’t have a child component. PD: Sorry for my english

Mario
12 years ago

How insert one custom message?