happybuddha
happybuddha

Reputation: 1358

How to fetch values from a properties file into Jsp/tagx

I am maintaining an application where in a couple of 100 Jsps and tagx files I need to replace a few hardcoded strings - the replacement values will be driven from a properties file already being read in.

The property file in my spring mvc app is being read in like so :

<context:property-placeholder location="classpath*:someProps.properties, someOther.properties" />

There is no id attribute that can be added to and I can't get the values through an id, so this option is out.

The only solution for this on the internet is to declare a PropertiesFactoryBean and then use the spring eval to read in a jsp/tagx. Something like this :

<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="singleton" value="true" />
        <property name="properties">
            <props>
                <prop key="database.name">${database.name}</prop>
            </props>
        </property>
    </bean>

This would soon become cumbersome if I need to read a lot of values (which looks like will be the case pretty soon in this app). Are there any other ways one could read the properties from a property file, in a jsp/tagx file ? It would also help me understand if someone can tell me the differences between PropertiesFactoryBean and context:property-placeholder ?

Spring version 3.2.2.RELEASE

Upvotes: 1

Views: 5477

Answers (2)

Stellar
Stellar

Reputation: 7

You can try i18n , add config information in the spring-servlet.xml:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- properties path -->                     
<property name="basename" value="messages" />              
<property name="useCodeAsDefaultMessage" value="true" />           

and in the jsp file, you just need add:<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> then you can get the value from .propertise file, like:

<spring:message code="hello" arguments="111,222" argumentSeparator=",">

Good luck!

Upvotes: 0

Prasad
Prasad

Reputation: 3795

You can get the property values in jsp by propertyplaceholder and using spring tag in jsp: In your context xml:

    <!-- PropertyPlaceHolder -->
    <util:properties id="propertyConfigurer" location="WEB-INF/test/someProps.properties"/>
    <context:property-placeholder properties-ref="propertyConfigurer"/>

In your jsp:

     <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    ...
    <spring:eval expression="@propertyConfigurer.getProperty('your.property1')" />

Upvotes: 1

Related Questions