Tuesday, April 9, 2013

JSF 2 Tips & Techniques - JSF Messages, Resource Bundle, and Validation Messages

Overriding default JSF error / warning Messages

Message bundle is used to override jsf's default error / warning messages. Declare message-bundle in your faces-config.xml and write new messages for default keys. You can define list of JSF defined messages from JSF specification under "Localized Application Messages"

Resource Bundle

Localized Resource bundle is defined with resource-bundle tag inside faces-config at global level or it is defined at the page level with loadBundle tag. Define resource bundle properties file in src/main/resources folder (You use any pacakge hierarchy e.g. if you want to define your resource bundle something like com.abc.view.ResourceBundle.properties then you needed to define corresponding folder hierarchy in your resource folder which will be src/main/resources/com/abc/view/ResourceBundle.properties. 

After defining your resource bundle, register it either on the page through f:loadBundle tag or globally in faces-config file using resource-bundle tag. 

Then Use it in your page like #{rb['app.title']}

Validation Messages using Bean Validation API

If you want to override default messages of Bean validation api or want to introduce new messages, then create ValidationMessages.properties file at root level i.e. at src/main/resources folder.

No comments: