Access Spring properties ?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Access Spring properties ?

Jan Goyvaerts
This post was updated on .
I've got the message resolvers working just fine on Spring.

However, I'd like to get some settings too on the screen. For instance build information of the application. Which is not in a resource bundle.

${build.version} Does not seem to work in a template. It does in a Spring config file however.

Is there a way to access the Spring settings ?

TIA !!!

Jan
Reply | Threaded
Open this post in threaded view
|

Re: Access Spring properties ?

danielfernandez
Administrator
I'm afraid resource bundles and configuration files are two very different things, only linked by the fact that they both usually persist their data in the form of .properties files.

Spring manages resource bundles and configuration in completely different manners: the former are made available to the application through implementations of the MessageSource interface (which is what thymeleaf delegates to), and the latter are made available through utility objects like PropertyPlaceholderConfigurer.

As you say, you can use SpringEL expressions in places like @Value annotations in order to extract values from configuration .properties files (by means of a PropertyPlaceholderConfigurer, usually), but resource bundles are not available in that context. In a similar way, configuration files are not made available to the view layer whereas resource bundles are.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Access Spring properties ?

niels
You can use the following trick in your spring config.
messageSource.setCommonMessages(furtherProperties);

Then you can access it with #{}. Perhaps it's useful to add a prefix to the keys.
Reply | Threaded
Open this post in threaded view
|

Re: Access Spring properties ?

Jan Goyvaerts
In reply to this post by danielfernandez
Well yes - it's different. But important to the gui nonetheless. Because some settings are locale-independent. For example a contact phone number you want to show in the "about" dialog. The url to report an error. The url for an image in the gui. Etc...

I've found a way, but a bit heavy on the markup:

(1) Add ApplicationContext.getEnvironment() as a variable to the template context. Ie env.
(2) In the markup the settings can be read as: th:version="${env.getProperty('build.version')}".

But I would really rather have ${build.version} work also.
Reply | Threaded
Open this post in threaded view
|

Re: Access Spring properties ?

niels
setCommonMessages are locale independent. How ever if you have an own solution it's fine.
Reply | Threaded
Open this post in threaded view
|

Re: Access Spring properties ?

Jan Goyvaerts
I'll have a try - the markup is far simpler indeed.

thanks !

On Sat, Apr 27, 2013 at 10:14 AM, niels [via Thymeleaf - User Forum] <[hidden email]> wrote:
setCommonMessages are locale independent. How ever if you have an own solution it's fine.


If you reply to this email, your message will be added to the discussion below:
http://forum.thymeleaf.org/Access-Spring-properties-tp4025970p4025989.html
To unsubscribe from Access Spring properties ?, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Access Spring properties ?

Jan Goyvaerts
*much* better !

Many thanks indeed for this hint ! :-)