跟着Spring in action这本书使用java配置方式搭Thymeleaf环境遇到中文乱码问题。在web.xml和WebConfig.java中设置编码格式都不能解决。最后使用了如下方面成功解决了:
- 在
ViewResolver
中.setCharacterEncoding("UTF-8");
- 在
TemplateResolver
中.setCharacterEncoding("UTF-8");
修改html页面后需要重新启动服务器才能看到修改界面,但是开发中不可能这么做。可以将Thymeleaf的cache关闭:
- 在
TemplateResolver
中`templateResolver.setCacheable(false);
最后附完整的WebConfig
package me.zji.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
impo