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}?
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- ...
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.
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.
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']}
.
After Spring 3.1, you can use <spring:eval />
tag with SpEL like this:
<spring:eval expression="@applicationProps['application.version']"
var="applicationVersion"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With