简体   繁体   English

Spring Boot MVC MessageSource defaultEncoding不起作用

[英]Spring Boot MVC MessageSource defaultEncoding does not work

Simple Spring Boot Web Application. 简单的Spring Boot Web应用程序。 Configs snippet: 配置代码段:

    @Bean
    public MessageSource validationMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("ValidationMessages");
        messageSource.setDefaultEncoding("UTF-8");

        return messageSource;
    }

    @Bean
    public Validator validator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setValidationMessageSource(validationMessageSource());
        return localValidatorFactoryBean;
    }

I set a defaultEncoding as UTF-8 . 我将defaultEncoding设置为UTF-8 I have ValidationMessages.properties file that is encoded in UTF-8 (my IntelliJ says so, notepad++ says so) and contains different characters. 我有ValidationMessages.properties文件,该文件以UTF-8编码(我的IntelliJ表示,notepad ++表示),并且包含不同的字符。

I have a simple web page (created using Freemarker). 我有一个简单的网页(使用Freemarker创建)。 There is some static UTF-8 text on that page and it is displayed correctly. 该页面上有一些静态UTF-8文本,并且可以正确显示。

And I want to see correct error messages when I submit a form located on that page, but when validation fails ( JSR-303/JSR-349 validation) I can see someting like обовÑзкове поле. min 2, max 15 ÑимволÑв 而且,当我提交位于该页面上的表单时,我希望看到正确的错误消息,但是当验证失败(JSR-303 / JSR-349验证)时,我会看到类似обовÑзкове поле. min 2, max 15 ÑимволÑв обовÑзкове поле. min 2, max 15 ÑимволÑв that's a message from .properties but it's wrongly encoded! обовÑзкове поле. min 2, max 15 ÑимволÑв这是来自.properties的消息,但编码错误!

Here is the page (simplified: no divs, css , js, etc.): 这是页面(简体:没有div,css,js等):

<#import "/spring.ftl" as spring/>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>ShareMyCar</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    </head>
    <body>
       <form action="/registration" method="POST">
           <@spring.formInput path="modelObj.name" />
           <@spring.showErrors "<br>"/>

           <input type="submit" class="btn btn-primary" value="Submit" />            
        </form>

    </body>
</html>

What else I need to do to make it work? 我还需要做些什么才能使其正常工作?

Java automatically assumes ISO-8859-1 encoding on localization bundles. Java自动在本地化包中采用ISO-8859-1编码。

See: https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.InputStream) 请参阅: https : //docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.InputStream)

To make it work, either save the file as ISO-8859-1, or use escape sequences such as \ሴ. 要使其正常工作,请将文件另存为ISO-8859-1,或使用转义序列,例如\\ u1234。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM