解决SpringBoot项目中Thymeleaf模板的中文乱码问题
1、使用IDEA创建SpringBoot项目




package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages="com.example")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
这里通过@ComponentScan(basePackages="com.example")来扫描包。
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(){
return "Hello Spring Boot^^^";
}
}
2、导入Thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3、读取properties文件
# application.properties
server.port=8080
server.servlet.context-path=/demo
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.messages.basename=i18/home

# home.properties、home_zh_CN.properties
welcome=欢迎您!
# home_en_US.properties
welcome=Welcome!
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/goToViewPage")
public ModelAndView passParametersWithModelAndView() {
ModelAndView modelAndView = new ModelAndView("viewPage");
modelAndView.addObject("message", "Baeldung");
return modelAndView;
}
}
<!-- viewPage.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf测试</title>
</head>
<body>
<P th:text="#{welcome}"></P>
</body>
</html>
注意:properties文件的编码格式必须是UTF-8。


解决SpringBoot项目中Thymeleaf模板的中文乱码问题的更多相关文章
- 解决docker容器中Centos7系统的中文乱码
解决docker容器中Centos7系统的中文乱码问题有如下两种方案: 第一种只能临时解决中文乱码: 在命令行中执行如下命令: # localedef -i zh_CN -f UTF-8 zh_CN. ...
- 解决springboot项目中@Value注解参数值为null的问题
1.错误场景: springboot项目中在.properties文件(.yml)文件中配置了属性值,在Bean中使用@Value注解引入该属性,Bean的构造器中使用该属性进行初始化,此时有可能会出 ...
- 解决springboot序列化 json数据到前端中文乱码问题
前言 关于springboot乱码的问题,之前有文章已经介绍过了,这一篇算是作为补充,重点解决对象在序列化过程中出现的中文乱码的问题,以及后台报500的错误. 问题描述 spring Boot 中文返 ...
- springboot项目中thymeleaf布局应用
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- idea解决springboot项目中log4j漏洞升级问题
最近阿里云团队发现log4j漏洞,危险级别:严重,相关资讯 https://m.sohu.com/coo/hsdt/506958086_355140 https://www.sohu.com/a/50 ...
- java web项目中打开资源文件中文乱码
1 java web项目中经常使用多模块管理.在某一个模块中添加了一些资源文件.但不是启动项目.有时候需要在程序中读取资源文件内容,打包后放到容器中就不能正常运行了.需要将所有资源文件放到启动项目的 ...
- 完美解决在Servlet中出现一个输出中文乱码的问题
@Override public void doPost(HttpServletRequest reqeust, HttpServletResponse response) throws Servle ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- SpringBoot项目中遇到的BUG
1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...
随机推荐
- MYSQL 之 JDBC(七):增删改查(五) DAO设计模式
Data Access Object,数据访问对象 what:访问数据信息的类.包含了对数据的CRUD(create.read.update.delete,增删改查)操作,而不包含任何业务相关的信息. ...
- 数据可视化之PowerQuery篇(十一)使用Power BI进行动态帕累托分析
https://zhuanlan.zhihu.com/p/57763423 上篇文章介绍了帕累托图的用处以及如何制作一个简单的帕累托图,在 PowerBI 中可以很方便的生成,但若仅止于此,并不足以体 ...
- Python Ethical Hacking - BACKDOORS(5)
File Download: A file is a series of characters. Therefore to transfer a file we need to: 1. Read th ...
- Oracle版本发布规划 (文档 ID 742060.1)
Oracle Database Release Schedule of Current Database Releases (文档 ID 742060.1) Oracle Database RoadM ...
- day10:函数的实参&形参
函数的参数 (参数:配合函数运算的值)参数: (1)形参:形式参数,在函数的定义处 (2)实参:实际参数,在函数的调用处 形参: 普通形参(位置) , 默认形参 , 普通收集形参 ...
- ztree : 增删改功能demo与自定义DOM功能demo的结合
最近有个项目要用ztree,需要用ztree自带的功能(增删改),也需要自定义DOM的功能(置顶). ztree的demo里有增删改的demo,也有自定义DOM的demo,但没有两者结合的. 所以我把 ...
- 不知道Linux内核到底长啥样?这幅漫画让你秒懂!
下面给大家分享一个[超全2020Linux学习教程],点击链接免费领取哦~ https://www.magedu.com/?p=84301&preview=true
- Shell基本语法---处理海量数据的sed命令
sed命令 shell脚本三剑客之一 处理时,把当前处理的行存储在临时缓冲区中,称为模式空间,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕.接着处理下一行,这样不断重复,直到 ...
- 题解 洛谷 P6142 【[USACO20FEB]Delegation P】
和赛道修建类似,先对\(k\)进行二分,将最值问题转化为判定问题. 在判定一个\(k\)是否合法时,贪心去考虑,一个节点下面的若干条链在合并时,一条链肯定和另一条使它合并后恰好满足长度限制的链合并最优 ...
- Monster Audio 使用教程(二)效果参数的保存
点击左上角主菜单按钮,点击[保存]菜单,即可保存当前的所有效果参数. [另存为]菜单,则是把当前参数另存一个名称,然后通过[切换效果]菜单,实现效果的切换. 独立保存单个音轨的效果 点击音轨对应的[ ...