springBoot + Thymeleaf:属性中的 UTF-8

问题描述 投票:0回答:1

我有这个配置文件:

@Configuration
@EnableWebSecurity
@EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true)
public class MvcConfig implements WebMvcConfigurer {


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

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @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

模板:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      th:lang="${#locale.language}" lang="en">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
...
</html>

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

html spring-boot spring-mvc thymeleaf spring-thymeleaf
1个回答
0
投票

使用外部编辑器将所有文件保存为UTF-8

© www.soinside.com 2019 - 2024. All rights reserved.