User Friendly Validation Error Message in JSF 2

By default, JSF validation error messages are prepended with the client ID of the component. This makes the error message completely unacceptable. In this article, we will learn how to progressively improve the error message.

The Default Behavior

Let us say that we have a controller class as follows.

public class HelloController {
    @Size(min=5, message="Name must be at least 5 letters long")
    private String name;

    //Getter setter etc...


And an input text for the “name” property in a form:

Name: <h:inputText id="name" value="#{}"/> 
<h:message for="name"/>

By default, the validation error message will look something like this.


Of course, this is far from ideal.

Using the “label” Attribute

A simple way to improve the error message is to add a label attribute to the component.

Name: <h:inputText id="name" value="#{}" label="Name"/> 
<h:message for="name"/>

Now, the error message will look like this.


That’s better. Still, it looks like a hack.

Override Default JSF Message

The root of the problem is that JSF by default attempts to prepend either the client ID or the label to the message followed by a “:”. The structure of the message is defined using the following message key:

javax.faces.validator.BeanValidator.MESSAGE={1}: {0}

Where {0} is the error message and {1} is the client ID. To fix the problem properly, we will need to override this message.

First, create your own message properties file. You can call it anything. We will call in We will put it in the root of the source folder. The file will look like this.


Then, register the file in faces-config.xml.

<faces-config ...>

Now, the error message will look like this.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.