Spring Boot整合Thymeleaf

Spring Boot整合视图层技术

在目前的企业级应用开发中,前后端分离是趋势,但是视图层技术还占有一席之地。Spring Boot对视图层技术提供了很好的支持,官方推荐使用的模板引擎是Thymeleaf, 不过像FreeMarker也支持,JSP技术在这里并不推荐使用。

整合Thymeleaf

Thyme leaf 是新一代Java 模板引擎,类似于Velocity、FreeMarker 等传统Java 模板引擎。与传统Java 模板引擎不同的是, Thymeleaf 支持HTML 原型,既可以让前端工程师在浏览器中直接打开查看样式, 也可以让后端工程师结合真实数据查看显示效果。同时, Spring Boot 提供了Thymeleaf自动化配置解决方案,因此在Spring Boot 中使用Thymeleaf 非常方便。Spring Boot 整合Thymeleaf主要可通过如下步骤:

  1. 创建工程,添加依赖
    新建一个Spring Boot 工程,然后添加spring-boot-starter-web 和spring- boot-starter-thymeleaf 依赖,代码如下:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 配置Thymeleaf
    Spring Boot 为Thymeleaf提供了自动化配置类ThymeleafAutoConfiguration,相关的配置属性在org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties 类,ThymeleafProperties 部分源码如下:
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
        private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
        public static final String DEFAULT_PREFIX = "classpath:/templates/";
        public static final String DEFAULT_SUFFIX = ".html";
        private boolean checkTemplate = true;
        private boolean checkTemplateLocation = true;
        private String prefix = DEFAULT_PREFIX;
        private String suffix = DEFAULT_SUFFIX;
        private String mode = "HTML";
        private Charset encoding = DEFAULT_ENCODING;
        private boolean cache = true;
        //...
}

由此配置可以看到,默认的模板位置在classpath:/templates,默认的模板后缀为.html 。如果使用IntelliJIDEA工具创建Spring Boot 项目, templates 文件夹默认就会创建。
当然,如果开发者想对默认的Thymeleaf 配置参数进行自定义配置,那么可以直接在application.properties 中进行配置,部分常见配置如下:

# 是否开启缓存,开发时可设置为false ,默认为true
spring.thymeleaf.cache=true
# 检查模板是否存在,默认为true
spring.thymeleaf.check-template=true
# 检查模板位置是否存在,默认为true
spring.thymeleaf.check-template-location=true
# 模板文件编码
spring.thymeleaf.encoding=UTF-8
# 模板文件位置
spring.thymeleaf.prefix=classpath:/templates/
#Content-Type配置
spring.thymeleaf.servlet.content-type=text/html
# 模板文件后缀
spring.thymeleaf.suffix=.html
  1. 配置控制器
    创建Book实体类,然后在Controller中返回ModelAndView,代码如下:
package org.gary.thymeleaf;

public class Book {
    private Integer id;
    private String name ;
    private String author;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

BookController.java

package org.gary.thymeleaf;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class BookController {
    @GetMapping("/books")
    public ModelAndView books() {
        List<Book> books = new ArrayList<>();
        Book b1 = new Book();
        b1.setId(1);
        b1.setAuthor("罗贯中");
        b1.setName("三国演义");
        Book b2 = new Book();
        b2.setId(2);
        b2.setAuthor("曹雪芹");
        b2.setName("红楼梦");
        books.add(b1);
        books.add(b2);
        ModelAndView mv = new ModelAndView();
        mv.addObject("books", books);
        mv.setViewName("books");
        return mv;
    }

}

代码解释:
• 创建Book 实体类,承载返回数据。
• 在BookController 中,第11~21 行构建返回数据, 第22-25 行创建返回ModelAndView ,设置视图名为books ,返回数据为所创建的List 集合。

  1. 创建视图
    在resources 目录下的templates 目录中创建books.html , 具体代码如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>图书列表</title>
</head>
<body>
<table border="1">
    <tr>
        <td>图书编号</td>
        <td>图书名称</td>
        <td>图书作者</td>
    </tr>
    <tr th:each="book:${books}">
        <td th:text="${book.id}"></td>
        <td th:text="${book.name}"></td>
        <td th:text="${book.author}"></td>
    </tr>
</table>
</body>
</html>

代码解释:
• 首先在第2 行导入Thymeleaf 的名称空间。
• 第14~18 行通过遍历将books 中的数据展示出来,Thymeleaf 中通过th:each 进行集合追历,通过th : text 展示数据。

  1. 运行
    在浏览器地址栏中输入“http://localhost:8080/books”, 即可看到运行结果,如图所示。
    在这里插入图片描述
    本节重点介绍Spring Boot 整合Thymeleaf,并非Thymeleaf 的基础用法,关于Thymeleaf 的更多资料,可以查看https://www.thymeleaf.org 。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值