views:

100

answers:

1

In my application context I have defined properties file:

<context:property-placeholder  location="classpath:application.properties" />

I want to get value of the property defined in that file on JSP page. Is there a way to do that in the way

${something.myProperty}?
+3  A: 

PropertyPlaceholderConfigurer can only parse placeholders in Spring configuration (XML or annotations). Is very common in Spring applications use a Properties bean. You can access it from your view this way (assuming you are using InternalResourceViewResolver):

<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list><value>classpath:config.properties</value></list>
    </property>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
    <property name="exposedContextBeanNames">
        <list><value>properties</value></list>
    </property>
</bean>

Then, in your JSP, you can use ${properties.myProperty}

Sinuhe
really good! But the first bean in declaration is of PropertiesFactoryBean type, not of ProperyPlaceholderConfigurer. Should this mean that in order to have replacement of property placeholders in xml I am to duplicate declaration of application properties in PropertyPlaceholderConfigurerBean?
glaz666
@glaz666: I forgot to say that PropertyPlaceholderConfigurer is not appropiate for this. I edited my answer a little.
Sinuhe
I have passed this "properties" bean to PlaceHolderConfigurer and it seems to work, but still I can't make it work in JSP files because when I try to access ${properties} it is even not trying to call getAttribute from ContextExposingHttpServletRequest where bean exposing happens
glaz666
@glaz666: Strange behaviour... Which version of Spring MVC are you using? ${...} works ok for other objects (such your controller results)?
Sinuhe
Ok, I have figured out what is going on. I am using sitemesh and when I am trying to refer to a bean in spring context on decorator page it is unavailable due to no spring's request wrapper is applied at that time. I have created another question on how to do that. http://stackoverflow.com/questions/3952755/how-to-obtain-model-attribute-in-sitemesh-decorator. Thanks for help here!
glaz666