Springboot中引入Thymeleaf

Thymeleaf作为spring官方推荐的模板引擎,在spring体系中使用异常方便。这里以gradle构建的项目为例来说明。
首先,你要先修改build.gradle引入Springboot对Thymeleaf提供的依赖包。在dependencies中增加如下配置。

compile('org.springframework.boot:spring-boot-starter-thymeleaf')

等待gradle帮你自己下载完依赖包后,你可以看到引入的Thymeleaf的版本。

Thymeleaf依赖包.png

嗯?springboot1.5.7默认引用的Thymeleaf依赖包居然还是2.1.5版本。最新的Thymeleaf不是已经更新3.x版本了么。如果我想使用最新版的Thymeleaf要怎么办呢。
在build.gradle文件中,buildscript下增加配置,完整的配置如下图

ext['thymeleaf.version'] = '3.0.7.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.2.2'

完整gradle配置.png

等待gradle下载完成,你可以看到引入的Thymeleaf已经是最新版本了。

快速入门

接下来要在项目中使用Thymeleaf了,这里用一个简单的单表查询来举个栗子。
一般来说,开发一个需要渲染数据的页面,分为三个步骤。

1.开发静态页面,即常说的模型。
2.获取数据。
3.使用数据对静态页面进行渲染。

这里我们先做第一个步骤,开发静态页面。为了简单,就不做任何css了,下面是页面的源码。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>标题</title>
</head>
<body>
  <strong>标题</strong>
  <form action="list.html" method="post">
    <input type="hidden" name="pageNumber">
    用户名:<input type="text" name="username">
    <br/>
    姓名:<input type="text" name="name">
    <br/>
    <button type="submit">提交</button> <button type="reset">重置</button>
  </form>
  <table>
    <thead>
      <tr>
        <th class="am-hide-sm-only">id</th>
        <th>用户名</th>
        <th>姓名</th>
        <th class="am-hide-sm-only">电话</th>
        <th class="am-hide-sm-only">邮箱</th>
        <th class="am-hide-sm-only">是否可用</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>id</td>
        <td>用户名&l
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值