楼主今天在学习SpringBoot集成thymelaf的时候报了中文乱码的错误,经过网上的搜索,现在得到解决的办法,分享给大家:

package com.imooc.config;

import org.springframework.beans.BeansException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver; /**
* WebMvc的配置类(自定义Thymeleaf模板)
*
* @author Liao Huan
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware { private ApplicationContext applicationContext; /**
* 设置上下文
*
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} /**
* Thymeleaf模板资源解析器(自定义的需要做前缀绑定)
*/
@Bean
@ConfigurationProperties(prefix = "spring.thymeleaf")
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setApplicationContext(this.applicationContext);
return templateResolver;
} /**
* Thymeleaf标准方言解释器
*/
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
//支持spring EL表达式
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
} /**
* 视图解析器
*/
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(templateEngine()); return thymeleafViewResolver;
}

我使用的是自定义的thymelefa模板,在配置文件中需要手动去配置上面的几个方法,这里给出thymeleaf部分配置文件和Controller类的截图代码:

applicaiton.properties:

然后测试Controller:

下面是我的HTML代码:

启动项目之后:出现中文乱码

解决办法如下图:(在最上面的配置文件相应位置加上下图红色箭头部分的代码)

重启项目即可解决中文乱码问题:

原文地址:https://blog.csdn.net/qq_32575047/article/details/82927873

最新文章

  1. Linux:U盘安装Linux系统
  2. C++网络编程 Java网络编程
  3. js替换指定字符串
  4. data-"mit.edu-Thinking In C++"
  5. #c word转换PDF
  6. HDU 1163 Eddy's digital Roots
  7. HW4.7
  8. html图像入门
  9. java复习(8)---I/O
  10. erlang二进制
  11. (最大连续和/最大子段和) P1115 最大子段和 洛谷
  12. java面试题:jvm
  13. javascript对象的属性,方法,prototype作用范围分析.
  14. ClientDataSet 心得
  15. stats.js随时查看fps
  16. [合集]解决Python报错:local variable 'xxx' referenced before assignment
  17. Hadoop单机Hadoop测试环境搭建
  18. codeforces 981 C.Useful Decomposition
  19. BZOJ2657:[ZJOI2012]旅游——题解
  20. layer.open中content里面的元素追加click事件,触发不了

热门文章

  1. SQL优化系列(一)- 优化SQL
  2. 2019-8-31-dotnet-core-用值初始化整个数组
  3. NOIP模拟 17.8.17
  4. 如何在IDE的开发环境中启动Studio和本地build出一个product
  5. laravel之文件上传
  6. QPS 提升60%,揭秘阿里巴巴轻量级开源 Web 服务器 Tengine 负载均衡算法
  7. AT2377 Blue and Red Tree
  8. Oracle错误——引发ORA-01843:无效的月份。
  9. Python基础:02数字
  10. 阿里云亮相2019联通合作伙伴大会,边缘计算等3款云产品助力5G时代产业数字化转型