How to use property from property file specified in PropertyPlaceholderConfigurer in JSP

26,757

Solution 1

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']}.

Solution 2

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

<spring:eval expression="@applicationProps['application.version']" 
             var="applicationVersion"/>
Share:
26,757
glaz666
Author by

glaz666

Updated on April 10, 2020

Comments

  • glaz666
    glaz666 about 4 years

    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}?