前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 整合 Thymeleaf

Spring Boot 整合 Thymeleaf

作者头像
村雨遥
发布2020-08-04 11:05:51
4690
发布2020-08-04 11:05:51
举报
文章被收录于专栏:JavaParkJavaPark

目录

  • 1. 什么是 Thymeleaf
  • 2. 整合过程
    • 2.1 添加 Thymeleaf 依赖
    • 2.2 编写实体类和 Controller
    • 2.3 创建 Thymeleaf 模板
    • 2.4 测试
  • 3. 注意事项

1. 什么是 Thymeleaf

  • Thymeleaf 是新一代的 Java 模板引擎,类似于 Velocity、FreeMarker 等传统引擎,其语言和 HTML 很接近,而且扩展性更高;
  • Thymeleaf 的主要目的是将优雅的模板引入开发工作流程中,并将 HTML 在浏览器中正确显示。同时能够作为静态引擎,让开发成员之间更方便协作开发;
  • Spring Boot 官方推荐使用模板,而且 Spring Boot 也为 Thymeleaf 提供了完整的自动化 配置解决方案;
  • Thymeleaf 使用教程请戳 Tutorial: Using Thymeleaf[1],配合 Spring 使用的教程请戳 Tutorial: Thymeleaf + Spring[2]

2. 整合过程

2.1 添加 Thymeleaf 依赖

添加 Thymeleaf 依赖有两种方式:

  1. 在新建项目时添加,在 Templeate Engines 中勾选 Thymeleaf;
  1. 对于未添加 Thymeleaf 依赖的项目,直接在 pom.xml 中手动添加依赖即可;
代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.2 编写实体类和 Controller

  1. 新建实体类 User
代码语言:javascript
复制
package com.cunyu.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : Author
 * @date : 2020/7/29 16:20
 * @description : User 实体类
 */

@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int age;
    private String name;
    private String email;
}
  1. 编写 Controller
代码语言:javascript
复制
package com.cunyu.controller;

import com.cunyu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : UserController
 * @date : 2020/7/29 16:22
 * @description : UserController
 */

@Controller
public class UserController {

    // 访问 ip:port/index
    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        // 设置跳转的视图
        modelAndView.setViewName("index");
        modelAndView.addObject("title", "Thymeleaf 使用");
        modelAndView.addObject("desc", "Spring Boot 整合 Thymeleaf");

        User author = new User(25, "村雨遥", "747731461@qq.com");

        modelAndView.addObject("author", author);
        return modelAndView;
    }
}

2.3 创建 Thymeleaf 模板

第 2.3 中,设置了跳转的视图为 index,所以我们需要在 src/main/resources/templates 中创建 index.html

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}"></title>

</head>
<body>
<h1 th:text="${desc}" th:align="center"></h1>
<h2 th:align="center">=====作者信息=====</h2>
<p th:text="${author?.name}"></p>
<p th:text="${author?.age}"></p>
<p th:text="${author?.email}"></p>
</body>
</html>

2.4 测试

启动项目,然后在浏览器中访问 http://localhost:8080/index,如果出现下图中的信息,说明整合成功。

3. 注意事项

为了方便使用,我们在使用 Thymeleaf 模板时,可以添加一些自己的配置;

代码语言:javascript
复制
# thymelea模板配置

# 设置模板文件存放位置
spring.thymeleaf.prefix=classpath:/templates/
# 设置模板后缀
spring.thymeleaf.suffix=.html
# 语法严格限制
spring.thymeleaf.mode=HTML5
# 编码格式
spring.thymeleaf.encoding=UTF-8
# 热部署,每次修改静态页面都不用重启就可以生效,默认为 true
spring.thymeleaf.cache=false

参考资料

[1]Tutorial: Using Thymeleaf: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

[2]Tutorial: Thymeleaf + Spring: https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 村雨遥 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • 1. 什么是 Thymeleaf
  • 2. 整合过程
    • 2.1 添加 Thymeleaf 依赖
      • 2.2 编写实体类和 Controller
        • 2.3 创建 Thymeleaf 模板
          • 2.4 测试
          • 3. 注意事项
            • 参考资料
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档