248 total views, 6 views today

JSF Resource Bundle Example

In JSF, we can create a property file and dump all the projects specific messages that we want to display in the browser in one place. Either it’s a simple welcome message or an error message your Java class is returning to the browser. Place all the message in a name-value pair in a property file.

This eliminates hard coding same message on multiple pages. Also, we can change the message text in the property file instead of changing it in multiple pages. This also supports internationalization that I will cover in next post.

We need to follow these rules to create a JSF Resource Bundle.

  • Create a <FILE_NAME>.properties under your Java package. The file must end with .properties
  • Register the property file in faces-config.xml file

 

Resource Bundle in JSF Example

 

As you see below, I have created the property file constants.properties under Java package com.catgovind .  The second arrow point to faces-config.xml file where we need to register the property file 

 

 

Resouce_Bundle_JSF JSF Resource Bundle Example

 

 

Register the property file with complete package name in the faces-config.xml file.  JSF appends the .properties automatically so don’t specify it here. The <var> parameter is a handle to access the properties. 

 

 

I have the following properties in the constant.properties file. The {0) and {1) are parameters here, we can pass values to this parameter at runtime 

 

 

Below is the Managed Bean class accessing a property from the Property file 

 

 

Below is the JSF page accessing the property file directly. Look at the handle message in the below JSF page should match with the handle message in faces-config.xml

 

 

Following is output in the Browser 

 

100117_1722_JSFResource1 JSF Resource Bundle Example

 

Download JSF Resource Bundle Project: JSF Resource Bundle Example

 

In my next post, I will cover the internationalization support in JSF 

govindan JSF Resource Bundle Example
Connect me

Govind

Thank you for visiting my personal blog. Myself Govindan, Software Developer by profession since 2006 and hence I started this blog early in 2016 and ever since I've been writing about technologies experienced and learnings of everyday life.

The views expressed on this blog are my personal views and do not necessarily reflect the views of my employer.

Please feeling free to reach me on any comments and feedbacks you have. Would be more than glad to listen and reply šŸ™‚
govindan JSF Resource Bundle Example
Connect me
By | 2017-10-01T12:54:12+00:00 October 1st, 2017|Categories: JSF|Tags: , |0 Comments
Like us on Facebook.
Connect!