Pankaj
Pankaj

Reputation: 3664

how can i use message with parameter using spring

I want to externalize application message to properties file. I'm loading the properties file using Spring.

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="locations">
  <list>      
     <value>classpath:applicationmessage.properties</value>      
  </list>
 </property>
 <property name="ignoreResourceNotFound" value="true" />

I have the message with para

message.myMessage = Couldn't find resource for customer id {0} and business unit {1}

What's the best way to read this message with parameter from java file ? Is there any other approach to externalize the messages.

Upvotes: 1

Views: 4764

Answers (2)

Dani
Dani

Reputation: 4111

It depends, exists differents ways to do, directly in jsp, in form validation process, etc..

For example

Message in properties:

msg=My message {0} and {1}.

In your jsp:

<spring:message code="msg" 
                arguments="${value1},${value2}" 
                htmlEscape="false"/>

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.message

Upvotes: 4

kalllx xia
kalllx xia

Reputation: 26

hi there are several ways to get properties message from spring.

way 1:

<util:properties id="Properties" location="classpath:config/taobaoConfig.properties" />

add this in spring.xml

in your java file . you create following property.

 @Resource(name = "Properties")
private Properties serverProperties;

the key-value in properties file will in serverProperties property.

way 2:

create a properties container bean

<bean id="propertyUtil" class="com.PropertiesUtil">
    <property name="locations">
        <list>
            <value>/WEB-INF/classes/datasource.properties</value>
            <value>/WEB-INF/classes/fileDef.properties</value>
        </list>
    </property>
</bean>

code of com.PropertiesUtil

    public class PropertiesUtil extends PropertyPlaceholderConfigurer {
private Properties properties;

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) {
    super.processProperties(beanFactory, props);
    this.properties = props;
}

/**
 * Get property from properties file.
 * @param name property name
 * @return property value
 */
public String getProperty(final String name) {
    return properties.getProperty(name);
}
}

you can use this container bean to get key-value in properties files .

Upvotes: 1

Related Questions