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模板的中文乱码问题的更多相关文章

  1. 解决docker容器中Centos7系统的中文乱码

    解决docker容器中Centos7系统的中文乱码问题有如下两种方案: 第一种只能临时解决中文乱码: 在命令行中执行如下命令: # localedef -i zh_CN -f UTF-8 zh_CN. ...

  2. 解决springboot项目中@Value注解参数值为null的问题

    1.错误场景: springboot项目中在.properties文件(.yml)文件中配置了属性值,在Bean中使用@Value注解引入该属性,Bean的构造器中使用该属性进行初始化,此时有可能会出 ...

  3. 解决springboot序列化 json数据到前端中文乱码问题

    前言 关于springboot乱码的问题,之前有文章已经介绍过了,这一篇算是作为补充,重点解决对象在序列化过程中出现的中文乱码的问题,以及后台报500的错误. 问题描述 spring Boot 中文返 ...

  4. springboot项目中thymeleaf布局应用

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  5. idea解决springboot项目中log4j漏洞升级问题

    最近阿里云团队发现log4j漏洞,危险级别:严重,相关资讯 https://m.sohu.com/coo/hsdt/506958086_355140 https://www.sohu.com/a/50 ...

  6. java web项目中打开资源文件中文乱码

    1 java web项目中经常使用多模块管理.在某一个模块中添加了一些资源文件.但不是启动项目.有时候需要在程序中读取资源文件内容,打包后放到容器中就不能正常运行了.需要将所有资源文件放到启动项目的 ...

  7. 完美解决在Servlet中出现一个输出中文乱码的问题

    @Override public void doPost(HttpServletRequest reqeust, HttpServletResponse response) throws Servle ...

  8. SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL

    1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...

  9. SpringBoot项目中遇到的BUG

    1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...

随机推荐

  1. VSCode下,项识别为 cmdlet、函数、脚本文件或可运行程序的名称。

    vscode下webpack错误:无法将“webpack”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. 解决方法: 1.因为 ...

  2. 【学习记录】C#保存数据至CSV文档 & DateTime格式模式控制解释

    数据类的定义: public class Result_Display { private string id; public string ID { get { return id; } set { ...

  3. 机器学习实战---决策树CART简介及分类树实现

    https://blog.csdn.net/weixin_43383558/article/details/84303339?utm_medium=distribute.pc_relevant_t0. ...

  4. 因为mac不支持移动硬盘的NTFS格式,mac电脑无法写入移动硬盘的终极解决办法

    相信很多实用mac的同学,都有磁盘容量问题,所以才使用移动硬盘 当移动硬盘在windows电脑上使用过之后,会被格式化为NTFS格式 而mac电脑不支持NTFS格式 这里有两种方法 第一种是把移动硬盘 ...

  5. 为什么在SpringBoot+maven的项目中,所引入的依赖包可以不指定依赖的版本号?

    当在Springboot项目中引入了spring-boot-starter-parent,则可以不用引入依赖包版本号,比如: <parent> <groupId>org.spr ...

  6. 关系型数据库查询语言 SQL 和图数据库查询语言 nGQL 对比

    摘要:这篇文章将介绍图数据库 Nebula Graph 的查询语言 nGQL 和 SQL 的区别. 本文首发于 Nebula Graph 官方博客:https://nebula-graph.com.c ...

  7. MySQL主从分离实现

    前言   大型网站为了减轻服务器处理海量的并发访问,所产生的性能问题,采用了很多解决方案,其中最主流的解决方案就是读写分离,即将读操作和写操作分别导流到不同的服务器集群执行,到了数据业务层,数据访问层 ...

  8. parted分区

    ((parted)mklabel ----创建磁盘标签New disk labeltype? gpt ---输入磁盘表情名(parted) p ----再次列出磁盘分区 (parted) mkpart ...

  9. pandas处理excel文件和csv文件

    一.csv文件 csv以纯文本形式存储表格数据 pd.read_csv('文件名'),可添加参数engine='python',encoding='gbk' 一般来说,windows系统的默认编码为g ...

  10. Bug--时区问题导致IDEA连接数据库失败

    打开cmd进入mysql,设置 set global time_zone='+8:00';