In this section, we will see how to use the default JSF validators to shoot out the built in error messages to the users.

Some of the points to keep in mind before we write the xhtml file are:

The h:message tag is used to display all the error messages related to the UI components.

This h:message has the following attributes

id attribute is the unique identifier for a ui component.

style displays the style information like color,font etc

for attribute describes the component name applicable for the form.

Let’s look in detail with an example as how to display the error messages

Create a JSF page named error.xhtml as

error.xhtml

In the above JSF page the required=true is set so that the field is mandatory and the validation message is displayed from a file called messages.properties which is present in the JSF jars.

For minimum and maximum length validation we use <c:validateLength> tag which validates against the minimum and maximum number of characters and prints the validations from the messages.properties file.

The validation for price field works in a similar way the difference being that the entered value is checked for double data type.

The radio button validator checks if the user has specified one of Petrol or Diesel engine and if none, an error is flashed on the screen. The date validator checks if a correct date format was entered by the user. In all these cases, we don’t have to write our own logic of field validation since JSF provides built in features for these kinds of common cases.

Now let’s create a managed bean Car.java as

Once done with this changes run the code and following output should be produced in the browser

JSF-Error-Messages-1-450x184

The project structure is as below image.

JSF_ErrorMessages_Project

This post is all about handling JSF Error Messages with the help of JSF validators. We will be looking into JSF Page Navigation in the coming tutorial. In the mean time, you can download the project from below link and play around with it to learn more.

By admin

Leave a Reply