frazkok
frazkok

Reputation: 49

How to store application specific JSF2.0 configuration parameters in a text file and store into session scoped bean during runtime

I've been searching the net but couldn't find a solution for this. I need help on how to store my application specific configuration parameters into a flat text file or xml file. Is there a java package or class that provides this service?

For example configuration for datatable pagination length, etc. So I don't have to recompile my application whenever I need to change the pagination parameters.

Something like this: Maybe myApp.cfg file which contain this. pagination: 10,20,30,50

And then in my xhtml file I would have something like this.

<p:dataTable id="dtClientList" value="#{saClientController.lazyModel}" rowsPerPageTemplate="#{myApp.cfg.pagination}">

And maybe even access the configuration parameter from within my session scoped or application scoped backing bean. Then reuse them for multiple users.

Is it possible to user resource bundle properties file to store configuration parameters such as pagination length and access the parameters into backing bean like this?

  FacesContext fc = FacesContext.getCurrentInstance();
    ResourceBundle bundle = fc.getApplication().getResourceBundle(fc,"bundle_name");
    bundle.getString("resource_identifier");

Im searching for something similar to app.config file in dot net implementation which is XML.

Please help. And if my question does not make sense, do let me know. Big Thank You.

Upvotes: 0

Views: 1124

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

You can use a properties file (simple text file) with content like this:

key1 = value1
key2 = value2

Name this file for instance MyResources.properties and put it in your source path root.

Then you can access a property file with the ResourceBundle class:

import java.util.ResourceBundle;
...
ResourceBundle rb = ResourceBundle.getBundle("MyResources");
String myValue1 = rb.getString("key1");

You can access this file from any session scoped bean you like. If the parameters are session independent, you could also use an application scoped bean instead.

Upvotes: 1

Related Questions