timmornYE
timmornYE

Reputation: 728

Where to put application settings in JSF

I wondered what is the prefered place to put application wide presettings in JSF. I see 3 options:

  1. web.xml
  2. properties file with <resource-bundle> configuration in faces-config.xml
  3. properties file and use it per java.util.Properties

I would prefer Nr. 2, because it seems to be most straightforward and easiest to use. But I am not sure if this is good, because in general I thought <resource-bundle> is planed for localized messages and not for application wide settings.

Upvotes: 1

Views: 2300

Answers (2)

BalusC
BalusC

Reputation: 1108802

web.xml

I gather you mean <context-param>? This is usually to be used for configuration parameters of servlet/filter based 3rd party libraries (JSF API/impl, PrimeFaces, OmniFaces, Jersey, etc).


properties file with <resource-bundle> configuration in faces-config.xml

This is usually to be used for localization of JSF pages, i.e. being able to switch to a completely different language site-wide.


properties file and use it per java.util.Properties

This is usually to be used for name=value pairs which can represent anything like localized text and configuration settings.


You see, there's not really a "which should I use" case here. The question "when should I use it" was been more appropriate. The answer is ultimately, just use the right tool for the job.

Upvotes: 4

noone
noone

Reputation: 19776

Option #1 and #2 have the advantage that you can also easily access the settings via EL. With option #1 you can do #{initParam['keyUsedForContextParam']}. With option #2 you can go the normal way for resource bundles #{rscBundleName['keyUsedInPropertiesFile']}. But as you already said: Most of the time you will use that for localization, so it will be a bit weird, but it would work. But for application wide settings, I think the web.xml with custom context-params would probably be the more appropriate way.

If you want to access the settings in your managed bean, you will have to access the settings via the FacesContext. For option #1 that might look like FacesContext.getCurrentInstance().getExternalContext().getInitParameter("keyUsedForContextParam");. This is not that nice.

Option #3 Also works of course, but you would need some kind of @ApplicationScoped utility bean, which will access the settings from your custom .properties-file. But that's actually not a very big problem. You could access the settings via EL like this: #{settingsBean.get('keyUsedInPropertiesFile')}. This is more obvious than initParam for example. In your managed beans you can also just inject your SettingsBean and access the settings the same way. Most easy to understand.

Option #3 also has a big not that obvious advantage. You can re-read the settings in real time. The web.xml and the resource bundles defined in the faces-config.xml will only be read on startup. The SettingsBean might re-read the .properties file every time and you would be able to change settings on-the-fly (make it @RequestScoped). The bean alternative is also more powerful in general, you might for example define default-settings which apply when you do not define any value in your properties file and so on.

I'd advise you to go with Option #3.

Upvotes: 5

Related Questions