SpringMvc + thymeleaf + HTML5 Chinese garbled problem,

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.