1

My application's Thymeleaf config is set as:

@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. 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. 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?

gtludwig
  • 5,411
  • 10
  • 64
  • 90

1 Answers1

2

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. Beats me why!

gtludwig
  • 5,411
  • 10
  • 64
  • 90
  • I have the same problem on `Spring Boot 4` with `Thymeleaf 2.1.5`, all files are set with `UTF-8`, just `messages_pt_BR.properties` go with `ISO-8859-1` – Thiago Pereira Jul 19 '16 at 14:50
  • Copy all contents to clipboard, delete the file from the file system, create a new file and ensure the desired encoding. Paste the contents into the newly created file and review for typos. It should work out fine! – gtludwig Jul 19 '16 at 17:25
  • Tks @gtludwig, i do this: http://stackoverflow.com/questions/31143923/eclipse-wrong-java-properties-utf-8-encoding?answertab=active#tab-top – Thiago Pereira Jul 19 '16 at 22:28