In my earlier posts, I explained how to get started with JSF2 and work with JSF View pages by adding HTML components such as labels, text fields etc. Today we will look into one of the most important component of any web framework i.e Resource Bundles and Custom Messages.

Resource Bundle

The phenomenon of storing the UI labels, date, status messages and other UI textual elements in a separate properties file instead of hardcoding these in a page is called resource bundling. It is the most convenient way of handling these kinds of resources as one can change these texts in the properties file instead of tinkering the xhtml file at a later stage.

Creating and invoking resource bundle involves the following steps

Create a properties file called resourcebundle.properties. You can check the property file location from the project structure image given below or by downloading the project.

resourcebundle.properties"

Create the JSF page by using the resource bundle to display the labels and error messages.

resourcebundle.xhtml

To render the individual xhtml files, right click on the jsf/xhtml file in NetBeans IDE and click on Run. Or, run the project and point the browser to the desired xhtml page. Notice that I am loading the resource bundle file, it’s similar to importing a java class.

Now create a managed bean named Car.java as

Once done with these changes run the project to render the JSF page and it should look as shown below.

Resource-Bundle-

As you can notice the labels are picked up from the properties file. Now if we want to change say ‘Color’ label to more clear ‘Car Color’, we just have to edit the resourcebundle.properties file which is easier than to change it in the xhtml file. This has clear advantages when the code is in production and we don’t want to meddle with the xhtml stuff which is already working very well.

Custom Messages

Custom messages in JSF allows users to define their own error messages conveniently. By default JSF provides some standard error messages depending on the context like say field type or field length validations. JSF field validations are an easier way of checking some common UI data correctness scenarios instead of writing JavaScript code or even JSP/java validations. We will discuss about the JSF default error messages elsewhere. Custom messages override these cryptic default messages with a more clear user defined one. Let’s understand how to configure our own error messages in the below example.

Create a JSF page named customerror.html as

customerror.html

The validateLength method is used for checking the length of characters by accepting minimum and maximum where we can specify the number of characters and then the h:message tag is used to print the error message. Also the required="true" indicates that the field is mandatory and the requiredMessage is used to print the message as specified by the user.

JSF framework has standard keys for length check minimum and maximum error message, we can override them by adding below properties to our resource bundle file.

Now if you are wondering how JSF will know where to look for our custom defined error messages, it’s configured in faces-config.xhtml file as shown below.

faces-config.xhtml

Once done with these changes run the application which displays the following output.

Resource-Bundle-

As we can see, there is no need of extra code to throw our own custom error messages since we are using built in JSF validations and the properties file to pick up the text.

The project structure is as shown below:

JSF-ResourceMessages-Project-311x450

This post is all about handling JSF Resources and Custom Messages. We will be looking into JSF Error Messages 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

%d bloggers like this: