PDA

View Full Version : Access PropertyPlaceholderConfigurer in JSP


jackysee
Jun 15th, 2005, 08:32 AM
As titled.

I have configured some properties files for the application using the PropertyPlaceholderConfigurer. How can I access these properties in JSP?

delnoij
Jun 15th, 2005, 11:18 AM
The PropertyPlaceholderConfigurer is implemented as a bean factory post-processor. It gets a callback from the BeanFactory after any initialization methods are called and is used to externalize some property values from a BeanFactory definition (such as the ApplicationContext defined in the applicationContext.xml file), into another separate file in Java Properties format.

I think the PropertyPlaceholderConfigurer was not meant for your usage scenario.

jackysee
Jun 15th, 2005, 01:57 PM
There is some deployment dependant properties which will be used by both beans and JSP. What should I use?

delnoij
Jun 15th, 2005, 02:33 PM
Use the PropertyPlaceholderConfigurer and properties files to set deployment specific values on your config files. Use the BeanFactory / ApplicationContext to configure and initialize your beans. Make your beans known in the Web tier by injecting them in your controllers, using setter or constructor injection.

Or describe your usage scenario in more detail :)

jackysee
Jun 16th, 2005, 04:09 AM
Those properties are some link to a remote server. In development environment, we will point it locally. In testing environment, we wil point it to a specific location. The Ant build would take care the choice of properties file when building the application.

I'm already injecting those properties to a handlerInterceptor but I also need them in the JSP view.

delnoij
Jun 16th, 2005, 05:28 AM
These data are part of your model. In the Web Tier, it is the responsibility of the Controller to provide the View with the Model, so I still think you should inject your beans containing your deployment specific config in your controller and add them to your Model using something like (in your Controller implementation):


private IConfigBean configBean;

public void setConfigBean(IConfigBean configBean) {
this.configBean= configBean;
}

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {
...
Map model = new HashMap();
model.put("configParam", configBean.getConfigParam1());
return new ModelAndView("myView", model);
}

Now the config parameter is made available to the View in your Model.

mykola
Jul 8th, 2007, 03:27 PM
http://j2eecookbook.blogspot.com/2007/07/accessing-properties-loaded-via-spring.html