SpringMvc + thymeleaf + HTML5 Chinese garbled problem,
SpringMvc + thymeleaf + HTML5 environment encounters Chinese garbled characters ......
Based on previous experiences, the development environment is UTF-8 encoded, and the server is configured with encoding filters. The front-end page is also UTF-8 encoded.
Depressed. What's wrong?
Troubleshooting 1: The developer tool checks the request and finds that the post Request body (form) is garbled.
The first pitfall: Form Add: accept-charset = "UTF-8", the request body is normal. The server also prints Chinese characters.
Troubleshooting 2:I think the server should be okay, but why is the page still garbled?
Baidu found that some people said that the file encoding is incorrect, that is, the file encoding is not the UTF-8 set in the page, so carefully check the other and found that the code is still garbled = |
Troubleshooting 3: Since the front-end html is okay and the server is okay, thymeleaf is naturally suspected.
Second pitfall: thymeleaf configuration problems (java file configuration method, the same xml configuration with the encoding is OK)
1: The template parser sets UTF-8 encoding.
1 @Bean 2 public ITemplateResolver templateResolver() { 3 ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); 4 templateResolver.setPrefix("/WEB-INF/templates/"); 5 templateResolver.setSuffix(".html"); 6 templateResolver.setTemplateMode("HTML5"); 7 templateResolver.setCharacterEncoding("utf-8"); 8 templateResolver.setCacheable(false); 9 return templateResolver;10 }
2: View parser sets UTF-8 encoding
1 @Bean2 public ViewResolver springThymeleafViewResolver() {3 4 ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();5 viewResolver.setTemplateEngine(templateEngine());6 viewResolver.setOrder(1);7 viewResolver.setCharacterEncoding("utf-8");8 return viewResolver;9 }
OK to solve the problem.