楼主今天在学习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

SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法的更多相关文章

  1. C#中WebClient使用DownloadString中文乱码的解决办法

    原文:C#中WebClient中文乱码的解决办法 第一次尝试: string question = textBox1.Text.ToString(); WebClient client= new We ...

  2. 详解get请求和post请求参数中文乱码的解决办法

    首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...

  3. Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧)

    干货:Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧) [解决办法]: 菜单栏中[File]->[Reload As E ...

  4. IDEA使用maven构建时控制台中文乱码的解决办法

    使用maven clean install 项目时控制台中文乱码,解决办法如下: Setting->maven->runner VMoptions: -Dfile.encoding=UTF ...

  5. resin后台输出中文乱码的解决办法!

    resin后台输出中文乱码的解决办法! 学习了:https://blog.csdn.net/kobeguang/article/details/34116429 编辑conf/resin.con文件: ...

  6. php使用curl获取文本出现中文乱码的解决办法

    在使用php的curl获取远程html文本时出现了中文乱码. 解决办法的代码如下: $url = "www.ecjson.com";//获取页面内容$ch = curl_init( ...

  7. get请求和post请求参数中文乱码的解决办法

    get请求参数中文乱码的解决办法 在tomcat的server.xml里的Connector加个URIEncoding="UTF-8",把 <Connector connec ...

  8. RoportNG报表显示中文乱码和TestNG显示中文乱码实力解决办法

    最近在进军测试自动化框架学习阶段,但无意间总是会伴随小问题的困扰,比如中文乱码,而导致显示总是不舒服,个人觉得,就一定要解决,似乎有点点强迫症.所以遇到RoportNG报表显示中文乱码和TestNG显 ...

  9. ReportNG报表显示中文乱码和TestNG显示中文乱码实力解决办法

    最近在进军测试自动化框架学习阶段,但无意间总是会伴随小问题的困扰,比如中文乱码,而导致显示总是不舒服,个人觉得,就一定要解决,似乎有点点强迫症.所以遇到ReportNG报表显示中文乱码和TestNG显 ...

随机推荐

  1. eNSP模拟器路由器无法正常启动一直显示“#”——问题解决方法

    eNSP模拟器路由器无法正常启动一直显示“#” 问题项如截图: 解决方案: 1. 打开自己电脑的控制面板 -->> 系统和安全 -->> Windows Defender防火墙 ...

  2. Python之常用模块1

    1.time datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理 ...

  3. List分页

    listObj.Skip((pagecount-1)*pagesize).Take(pagesize) 假设你每页10条数据当前是第3页 跳到第4页则:listObj.Skip((4-1)*10).T ...

  4. Spark-day01

      Spark初始 什么是Spark Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎.Spark是UC Berkeley AMP lab (加州大学伯克利分校的AMP实验室) ...

  5. pl/sql基础知识—过程快速入门

    n  过程 过程用于执行特定的操作,当建立过程时,既可以指定输入参数(in),也可以指定输出参数(out),通过在过程中使用输入参数,可以将数据传递到执行部分:通过使用输出参数可以将执行部分的数据传递 ...

  6. Map容器案例

    案例讲解  --统计字符串出现的次数 package com.date; import java.util.HashMap; import java.util.Map; import java.uti ...

  7. nodeJs学习-13 router

    const express=require('express'); var server=express(); //目录1:/user/ var routeUser=express.Router(); ...

  8. celery琐碎笔记

    -l 指定日志等级 -n 指定任务名称 -Q 指定任务执行队列 -c 指定启动celery的cpu数量 --logfile 指定日志输出到文件,会输出任务函数里的print,而控制台不会,用于调试. ...

  9. 如何将英文PDF文献翻译成中文

    方法一:利用Google的本地文档翻译功能 这种方法比较简单,打开Google翻译首页http://translate.google.cn/, 选择"上传文档" 选择文件,开始翻译 ...

  10. keep-alive vue组件缓存避免多次加载相应的组件

    keep-alive vue组件缓存避免多次加载相应的组件