Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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}?
like image 834
glaz666 Avatar asked Oct 14 '10 13:10

glaz666


People also ask

How read value from properties file in JSP?

My approach would be to read the properties file in normal Java code, put the name-value pairs in a Map or List of objects that represent Name-Value pairs, put the Map or List in the request context (setAttributes), then iterate over the List/Map in your JSP so that the property names and values are embedded in JSP- ...

How do you load the data from the properties file?

The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load . properties file in the form of key-value pairs.

How do you externalize a constants from a Spring configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.


2 Answers

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

like image 166
sinuhepop Avatar answered Sep 19 '22 08:09

sinuhepop


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

<spring:eval expression="@applicationProps['application.version']" 
             var="applicationVersion"/>
like image 21
btpka3 Avatar answered Sep 21 '22 08:09

btpka3