简体   繁体   English

如何使用spring使用带参数的消息

[英]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.

相关问题 如何使用MessageListenerAdapter在Spring amqp中将Pojo与消息类型参数一起使用? - How to use pojo with Message type parameter in spring amqp using MessageListenerAdapter? 如何在 Spring Boot 的查询注释中使用我的参数? - How can I use my parameter in query annotation in Spring Boot? 如何使用 Spring Boot 返回成功消息? - How can I return a successful message using Spring Boot? 我如何在构造函数中为未经过Spring修饰的对象使用autowired参数? - How can I use autowired parameter in a constructor for an object that isn't Spring-ified? 我可以在没有 Message Broker 的情况下使用 Spring Integration 吗 - Can I use Spring Integration without Message Broker 为什么我不能使用jsp:include在Spring MVC中传递参数? - Why I can not use jsp:include to pass a parameter in Spring MVC? 如何在 Java Spring 中使用没有 @KafkaListener 的 KafkaTemplate 使用来自 Kafka 的消息? RabbitMQ 模拟 - How I can consume message form Kafka using KafkaTemplate without @KafkaListener in Java Spring? RabbitMQ analogue 使用 Spring 时,如何抑制 checkstyle 消息“Utility classes should not have a public of default constructor” - How can I supress the checkstyle message "Utility classes should not have a public of default constructor" when using Spring 如何使用Spring MVC将表单参数绑定到Java 8 Duration? - How can I bind a form parameter to a Java 8 Duration using Spring MVC? 如何在zk中使用此参数? - How can I use this parameter with zk?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM