【问题标题】:Spring Boot CRUD Application with Thymeleaf -UTF-8 in property files属性文件中带有 Thymeleaf -UTF-8 的 Spring Boot CRUD 应用程序
【发布时间】:2021-03-28 06:47:55
【问题描述】:

我有一个 SpringBoot 应用程序。这个百里香模板:

        <!DOCTYPE html>
        <html xmlns:th="http://www.thymeleaf.org">
        <head>
            <meta charset="utf-8">
        ...
                               <li><label th:text="#{view.age}">
</label><span>25</span></li>
    
    âge:
        
        ...

关于属性文件:

view.age=âge:

但是在浏览器上我看到了这个:

âge:25

âge:

在配置文件中:

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.US);
    return slr;
}

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName("lang");
    return lci;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}

在 application.properties 上:

    spring.messages.encoding=UTF-8
spring.thymeleaf.encoding=UTF-8

IntelliJ IDEA 也设置为 UTF-8

@Override
    protected void configure(HttpSecurity http) throws Exception {

        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();

        encodingFilter.setEncoding("UTF-8");
        encodingFilter.setForceEncoding(true);

        http.addFilterBefore(encodingFilter, CsrfFilter.class);
..
}

我也尝试将view.age=&amp;acirc;ge: 放在属性文件中,但后来我在浏览器中看到&amp;acirc;ge:

【问题讨论】:

    标签: html spring spring-boot utf-8 thymeleaf


    【解决方案1】:

    我认为您几乎可以配置...只有几个额外的想法。

    首先,请注意spring.messages.encoding 属性仅在您的配置中do not define 自己的MessageSource 并且Spring Boot 可以应用默认消息源自动配置时才有效。

    如果你定义了你的MessageSource if 应该有适当的编码(default 它是iso-8859-1):

    @Bean
    public ResourceBundleMessageSource messageSource() {
      ResourceBundleMessageSource messageSource = 
        new ResourceBundleMessageSource();
      messageSource.setBasename("messages"); // Please, set the right value
      messageSource.setDefaultEncoding("UTF-8"); // Set the UTF-8 encoding
      return messageSource;
    }
    

    您可以从不同的角度来看待它:如果您还没有这样做,也许在您的配置中包含一个像上面描述的自定义 MessageSource 也可能是解决问题的方法。

    此外,请确保属性文件采用所需的编码:如果您以某种编码打开文件,Intellij 可能会保留该编码。尝试使用所需的编码重新加载文件,UTF-8,如图所示。请注意,在许多情况下,此过程具有破坏性,它会更改您的属性文件的内容,并且您可能需要编辑一些字符。您当然可以使用正确的编码从头开始重新创建属性文件。

    虽然我认为问题与属性文件的编码有关,但您也可以尝试设置为您的模板配置的ThymeleafViewResolver的字符编码,并指出其字符编码和内容类型:

    // In the way you have configured the view resolver for Thymeleaf
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    // the rest of your configuration
    viewResolver.setCharacterEncoding("UTF-8");
    viewResolver.setContentType("text/html; charset=UTF-8");
    

    您可以尝试的最后一件事是在属性文件的内容中包含转义序列。

    通常,这是在您的属性文件具有默认编码 iso-8859-1 时完成的,并且您需要使用 JDK 提供的工具 native2ascii

    native2ascii [ inputfile ] [ outputfile ]
    

    请注意,您可能需要两个文件,一个是您像往常一样在其中编写带有特殊字符的属性文件,另一个是在将native2ascii 命令应用于第一个文件后获得的转义序列。第二个文件是您需要在 Spring 中配置为 i18n 资源的文件。

    遵循相同的想法,如果您使用 IntelliJ,您可以通过在属性文件的“设置/首选项”对话框的“文件编码”页面上选择相应的复选框来启用 Transparent native-to-ascii conversion。这将在后台处理提供的特殊字符和相应的转义序列之间的转换。请查看相关的documentation

    正如我之前告诉您的,请注意启用此特性会修改您的属性文件,因此请确保有一个备份副本,或者在您提交对源代码管理的更改时将其考虑在内。

    【讨论】:

    • @enLopes 我更新了答案,为您提供一种基于自定义 messageSource 的定义/配置的新方法。我希望它有所帮助。
    【解决方案2】:

    请尝试在您的 application.properties 中设置以下内容:

    spring.messages.encoding=UTF-8
    

    如果有用请告诉我。

    顺便说一句:还有一个几年前关于类似主题的帖子SpringBoot - UTF-8 Doesnt work in messages.properties

    【讨论】:

      【解决方案3】:

      模板编码将通过配置 application.properties 文件起作用 -

      spring.thymeleaf.encoding=UTF-8

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-12
        • 2015-12-13
        • 2016-09-23
        • 2018-05-02
        • 2016-01-08
        • 1970-01-01
        • 2021-08-21
        相关资源
        最近更新 更多