31 July 2009

JSF custom properties file

When building web apps in JSF, you often need some global values, such as server paths, email addresses, messages, etc. Hardcoding these in your pages doesn't make a whole lot of sense; the values might change over time or from server to server. Also, cluttering up the built-in JSF properties files isn't the most ideal solution.

The answer is to use custom properties files and load them using the loadBundle tag. Say you've created a properties file called CustomProps.properties in the package com.companyname.resources. In your JSF page or your template file, use the following to load the file:
<f:loadBundle basename="com.companyname.resources.CustomProps" var="props" />
If you have a property called hellomessage in the file, you reference it using the props variable:
<h:outputText value="#{props.hellomessage}" />

No comments: