【问题标题】:What is the function of setDefaultEncoding in ReloadableResourceBundleMessageSourceReloadableResourceBundleMessageSource中setDefaultEncoding的作用是什么
【发布时间】:2016-05-09 02:08:41
【问题描述】:

AFAIK,Spring 的 ResourceBundleMessageSource 建立在标准 J2SE java.util.ResourceBundlejava.util.Properties 之上,它们无法处理 ISO-8859-1 以外的文件编码。 但在许多示例代码中,我看到了这一点:

ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
resource.setBasename("classpath:messages");
resource.setDefaultEncoding("UTF-8");

resource.setDefaultEncoding("UTF-8"); 行的作用是什么?这让我很困惑,谁能解释一下?

【问题讨论】:

    标签: java spring-mvc utf-8 character-encoding properties-file


    【解决方案1】:

    属性在ReloadableResourceBundleMessageSource中加载如下:

    this.propertiesPersister.load(props, new InputStreamReader(is, encoding));
    

    查看spring-context source code。在进一步的课程中,通过java.util.Propertiespublic void load(Reader reader) 方法读取文件,该方法将编码感知InputStreamReader(is, encoding) 作为参数。

    这意味着常规的java.util.Properties 也可以加载不同的编码。

    示例

    PropertyReader.java

    package com.example;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Locale;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class PropertyReader {
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
            List<String> locales = new ArrayList<String>(Arrays.asList("en", "ru", "de"));
            for (String loc : locales) {
                Locale locale = new Locale(loc);
                String value = context.getMessage("example", null, locale);
                System.out.println(value);
            }
            ((AbstractApplicationContext) context).close();
        }
    }
    

    context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename">
                <value>messages</value>
            </property>
            <property name="defaultEncoding">
                <value>UTF-8</value>
            </property>
        </bean>
    
    </beans>
    

    messages.properties

    example=Example
    

    messages_de.properties

    example=Beispiel
    

    messages_ru.properties

    example=образец
    

    没有默认编码的结果

    注释 context.xml 中的属性

    Example
    обÑазеÑ
    Beispiel
    

    默认编码的结果

    Example
    образец
    Beispiel
    

    程序化方法的结果相同。

    【讨论】:

    • 但是当我 setDefaultEncoding("UTF-8") 时,属性文件包含 utf-8 字符,结果显示那些 utf-8 字符的问号
    • 刚刚用一个小例子更新了我的答案,如果你想自己测试一下。
    猜你喜欢
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2013-12-08
    • 2012-04-27
    • 2017-10-26
    • 2014-04-25
    • 1970-01-01
    相关资源
    最近更新 更多