[英]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. 我正在使用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 我有与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 ? 使用java文件中的参数读取此消息的最佳方法是什么? Is there any other approach to externalize the messages. 是否有任何其他方法来外化消息。
hi there are several ways to get properties message from spring. 嗨有几种方法可以从spring获取属性消息。
way 1: 方式1:
<util:properties id="Properties" location="classpath:config/taobaoConfig.properties" />
add this in spring.xml 在spring.xml中添加它
in your java file . 在你的java文件中。 you create following property. 你创建以下属性。
@Resource(name = "Properties")
private Properties serverProperties;
the key-value in properties file will in serverProperties property. 属性文件中的键值将在serverProperties属性中。
way 2: 方式2:
create a properties container bean 创建属性容器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 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 . 您可以使用此容器bean来获取属性文件中的键值。
It depends, exists differents ways to do, directly in jsp, in form validation process, etc.. 它依赖于,存在不同的方式,直接在jsp中,在表单验证过程中等。
For example 例如
Message in properties: 属性中的消息:
msg=My message {0} and {1}.
In your jsp: 在你的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 http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.message
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.