The standard JSF conversion and validation error messages are too detail, technical or sometime, not really human readable. In this article, it shows you how to customize standard conversion or validation error message in JSF 2.0.
Summary Guide
- Find your message key from jsf-api-2.x.jar, “Messages.properties” file.
- Create your own properties file, and put the same message key you found in above “Messages.properties” file, and override it with your custom error message.
- Register your properties file in “faces-config.xml”, put it as application level.
- Done.
1. Messages.properties
All JSF standard conversion and validation error messages are stored in “Messages.properties” file, which can be located from jsf-api-2.x.jar, “javax\faces\Messages.properties“, see figure below :

See portion of this “Messages.properties” file
...
# ===================================================================
# Converter Errors
# ===================================================================
javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' could not be understood as a date.
javax.faces.converter.DateTimeConverter.DATE_detail={2}: ''{0}'' could not be understood as a date. Example: {1}
...
# ====================================================================
# Validator Errors
# ====================================================================
javax.faces.validator.LengthValidator.MAXIMUM={1}: Validation Error: Length is greater than allowable maximum of ''{0}''
javax.faces.validator.LengthValidator.MINIMUM={1}: Validation Error: Length is less than allowable minimum of ''{0}''
...
For example,
1. <f:validateLength minimum=”5″ maximum=”10″ />
If maximum length validation failed, JSF gets “javax.faces.validator.LengthValidator.MAXIMUM”.
If minimum length validation failed, JSF gets “javax.faces.validator.LengthValidator.MINIMUM”.
2. <f:convertDateTime pattern=”d-M-yyyy” />
If date validation failed, JSF gets “javax.faces.converter.DateTimeConverter.DATE_detail”.
If you do not sure which key match to which validator tag, just display the error message once and compare it with “Messages.properties”, then you will know which key you want to override.
2. Custom Error Message
Create a properties file named “MyMessage.properties” (can be any name you like), put message key and custom error message inside. Later, put this properties file into your project resources folder.
MyMessage.properties
javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' could not be understood as a date.
javax.faces.converter.DateTimeConverter.DATE_detail=Invalid date format.
javax.faces.validator.LengthValidator.MINIMUM=Minimum length of ''{0}'' is required.
Now, you are going to custom the validation error message for “javax.faces.validator.LengthValidator.MINIMUM” and conversion error message for “javax.faces.converter.DateTimeConverter.DATE_detail“.
For the XXX_detail message key, you have to override its parent key (summary message) as well, which is XXX without the “_detail” behind; Otherwise, JSF will ignore your new custom error message and keep getting the standard error message from “Messages.properties”, may be this is a bug in JSF 2.0?
3. Register Message Bundle
Register your custom properties file in “faces-config.xml”, put it as Application level.
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<message-bundle>
com.mkyong.MyMessage
</message-bundle>
</application>
</faces-config>
4. Demo
A JSF page, add both <f:validateLength /> and <f:convertDateTime /> validation.
<?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>Customize validation error message in JSF 2.0</h1>
<h:form>
<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 DOB :
<h:inputText id="dob" value="#{user.dob}"
size="20" required="true" label="Date of Birth">
<f:convertDateTime />
</h:inputText>
<h:message for="dob" style="color:red" />
</h:panelGrid>
<h:commandButton value="Submit" action="result" />
</h:form>
</h:body>
</html>
When validation failed, display your custom error message now.

Pingback: free classified ads()
Pingback: weblink()
Pingback: link()
Pingback: here()
Pingback: pay plan()
Pingback: water ionizer()
Pingback: pay day loans()
Pingback: payment plan()
Pingback: payment plan()
Pingback: HD Coloring Pages()
Pingback: house blue()
Pingback: water ionizer()
Pingback: local 1 plumbers apprentice wages()
Pingback: locksmith dallas 75216()
Pingback: alkaline water brands()
Pingback: pay per day loans plan()
Pingback: Instagram likes kopen()
Pingback: laan penge()
Pingback: alkaline water benefits()
Pingback: car parking()
Pingback: water ionizers()
Pingback: parking()
Pingback: fue()
Pingback: lane penge nu()
Pingback: DIRECTV Satellite Television()
Pingback: Direct TV vs Cable TV()
Pingback: Blue Coaster33()
Pingback: Proxy Servers » JSF – Addding custom validation messages is causing issue in loading css files from resources()
Pingback: JSF 2.0 Tutorials()