简体   繁体   English

如何在Thymeleaf + Spring + Tomcat中启用UTF-8支持?

[英]How can I enable UTF-8 support in Thymeleaf + Spring + Tomcat?

My application's Thymeleaf config is set as: 我的应用程序的Thymeleaf配置设置为:

@Bean
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setCacheable(false);
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setPrefix(HTML_VIEWS);
    templateResolver.setSuffix(".html");

    return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addDialect(new SpringSecurityDialect());
    templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
    templateEngine.setTemplateResolver(templateResolver());

    return templateEngine;
}

@Bean
public ViewResolver viewResolver() {

    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setCharacterEncoding("UTF-8");
    viewResolver.setCache(false);
    viewResolver.setOrder(1);

    return viewResolver;
}

OS (CentOS 7) default encoding is UTF-8 and all generated files get UTF-8 enconding. 操作系统(CentOS 7)的默认编码为UTF-8,并且所有生成的文件都将获得UTF-8编码。 Still I cannot seem to be able to display characters correctly. 我似乎仍然无法正确显示字符。 I tried a bunch of suggestions found here, to no avail. 我尝试了一些在这里找到的建议,但没有成功。

I have also tried, as per some suggestions here, setting a CharacterEncodingFilter prior to CsrfFilter in my Spring Security configuration. 根据这里的一些建议,我还尝试了在Spring Security配置中在CsrfFilter之前设置CharacterEncodingFilter Also, the app is persisting the data WITH the strange characters. 此外,应用程序还会使用奇怪的字符来保留数据。

My securitu configuration starts with: 我的安全配置开始于:

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

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

    http.addFilterBefore(encodingFilter,CsrfFilter.class);
    // more security configs
}

What am I missing? 我想念什么?

The damned messages_pt_BR.properties file had a different encoding from the rest of the app... It decided to go with ISO-8859-1. 该死的messages_pt_BR.properties文件的编码与应用程序的其余部分不同...它决定采用ISO-8859-1。 Beats me why! 打我为什么!

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

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