按照以往经验逐个排查,开发环境统一为utf-8编码,服务器也配置了编码过滤器。前台页面也是utf-8编码的。
郁闷,到底哪里出错了呢?
排错1:开发者工具检查请求,发现我的post请求体(表单)乱码
排错2:感觉服务器应该没问题了,可是页面怎么还是乱码?
百度查有人说可能是文件编码不对,即文件编码不是页面中设置的utf-8,于是仔细检查另存一下,发现依旧乱码 ==|
排错3:既然前台html没问题,服务器也没问题,那么自然而然的怀疑到了thymeleaf身上了
1:模板解析器设置编码utf-8
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:视图解析器设置编码utf-8
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,解决问题。
新闻热点
疑难解答