15

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}?
glaz666
  • 8,707
  • 19
  • 56
  • 75

6 Answers6

38

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} or ${properties['my.property']}.

sinuhepop
  • 20,010
  • 17
  • 72
  • 107
  • 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 Oct 15 '10 at 08:10
  • @glaz666: I forgot to say that PropertyPlaceholderConfigurer is not appropiate for this. I edited my answer a little. – sinuhepop Oct 15 '10 at 09:28
  • 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 Oct 15 '10 at 13:36
  • @glaz666: Strange behaviour... Which version of Spring MVC are you using? ${...} works ok for other objects (such your controller results)? – sinuhepop Oct 15 '10 at 13:59
  • 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 Oct 17 '10 at 10:04
  • is this possible with spring 2.0.x? I'm getting the following error on Tomcat startup : Invalid property 'exposedContextBeanNames' of bean class [org.springframework.web.servlet.view.InternalResourceViewResolver]: Bean property 'exposedContextBeanNames' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? – Arvind Sridharan May 30 '12 at 06:46
  • 1
    @VishalZanzrukia Properties implements Map, so you can use ${properties['i.want.chicken.now']} – sinuhepop Jun 13 '14 at 09:39
6

After Spring 3.1, you can use <spring:eval /> tag with SpEL like this:

<spring:eval expression="@applicationProps['application.version']" 
             var="applicationVersion"/>
btpka3
  • 3,720
  • 2
  • 23
  • 26
0

To use recursive property placeholder expansion in views, you need a different solution, take a look at this answer:

https://stackoverflow.com/a/10200249/770303

Community
  • 1
  • 1
anttix
  • 7,709
  • 1
  • 24
  • 25
0
`<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
  id="messageSource"
  p:basenames="WEB-INF/i18n/site"
  p:fallbackToSystemLocale="false"/>`

Now this is your Properties File

`site.name=Cool Bananas`

And. Here goes your JSP

`<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
  <head>
    <title><spring:message code="site.name"/></title>
  </head>
  <body>
  </body>
</html>`
Nikhil Kotak
  • 307
  • 1
  • 4
  • 5
0

To use with multiple locations in a list which might not be present as can be done with the context:property-placeholder bean:

<beans:bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <beans:property name="ignoreResourceNotFound" value="true" />
    <beans:property name="locations">
        <beans:list>
            <beans:value>classpath:application.properties</beans:value>
            <beans:value>classpath:environment.properties</beans:value>
            <beans:value>classpath:environment-${env}.properties</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>
Matt
  • 11
  • 3
-4

This will show you the tables of the current schema (which you are logged in):

select table_name from user_tables order by table_name;

This will show you the tables of schema , for which you have select rights at least:

select owner, table_name from all_tables where owner='<owner>' order by owner, table_name;
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79