layout: post

SpringBoot手动配置thymeleaf模板引擎

subtitle: SpringBoot手动配置thymeleaf模板引擎 date: 2020-05-18 author: BY header-img: img/post-bg-kuaidi.jpg catalog: true tags: - java - springboot - thymeleaf

SpringBoot手动配置thymeleaf模板引擎

依赖

<!-- thymeleaf -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

控制器 注意删除: @ResponseBody 才能渲染页面

@RequestMapping(value = "/", method = RequestMethod.GET)
// @ResponseBody
public String index() {
  return "index";
}

默认模板文件位置和在源文件位置, resources文件夹和java文件夹平级

YWrtiQ.png

index.html文件引用资源文件

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">

<head>
  <title>SpringBoot模版渲染</title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <script src="/common.js" th:href="@{/common.js}"></script>
</head>

<body>
  index
</body>

</html>

src\main\resources\application.yaml

spring:
  thymeleaf:
    cache: false
    suffix: .html
    encoding: UTF-8
    mode: HTML