四、SpringBoot整合Thymeleaf

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_38323872/article/details/98874428

概述

Thymeleaf是一个与Velocity、FreeMarker类似的模板引擎,它是一个xml/xhtml/html5的模版引擎,可以作为MVC的Web应用的View层。Thymeleaf还提供了额外的模块与SpingMVC集成,所以我们可以使用Thymeleaf完全替代JSP。

整合Thymeleaf基本流程

1. 添加Thymeleaf启动类,POM文件如下:

        .......
        <!-- Thymeleaf启动类 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        ......

:也可以在创建项目的时候,选中想要初始化的配置,入下图:
在这里插入图片描述

2. 指定视图文件目录

在本猿的第一篇文章中有说到,src/main/resources/templates目录下的文件只能服务器内部来访问,因此该目录下的资源文件会相对安全,因此这里建议将Thymeleaf模板文件放到当前目录下。
在这里插入图片描述

3. 编写Controller层代码

package com.example.demo.helloworld.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * FileName: ThymeleafController
 * Author:   RollerRunning
 * Date:     2019/8/8 2:41 PM
 * Description: 解析数据并返回一个Thymeleaf模板.html
 * <p>
 * 注:这里不能使用@RestController注解,因为@RestController = @Controller + @ResponseBody
 */
@Controller
public class ThymeleafController {

    @RequestMapping("/learn")
    public String hello(Model model, String message) {
        System.out.println("接收到的前端数据为:" + message);
        model.addAttribute("result", "Message Is: " + message);
        return "hello";
    }
}

4. 编写Thymeleaf模板代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>my thymeleaf html</title>
</head>
<body>
    <span th:text="接口返回的数据:"></span>
    <span th:text="${result}"></span>
</body>
</html>

至此,一个简单的SpringBoot整合Thymeleaf的demo就完成了,啥玩意到了SpringBoot这,都会变得极其简单…amazing~~~

Thymeleaf基础语法

1. 字符串与变量常用操作

标记 作用
th:text 将变量值输出到页面
th:value 将值填充至input标签的value中
${#strings.isEmpty(key)} 判断字符串是否为空
${#strings.contains(msg,‘key’)} 判断字符串是否包含指定的子串
${#strings.length(msg)} 判断字符串长度
${#strings.substring(msg,5)} index为0,截取定长字符串
${#strings.substring(msg,5,8)} 截取指定范围字符串

2. if条件判断

    <span th:if="${result} == 'success'">
        请求成功!
    </span>
    <span th:if="${result} == 'error'">
        请求错误!
    </span>

3. switch判断

<div th:switch="${id}">
    <span th:case="1">RollRunning</span>
    <span th:case="2">SpringBoot</span>
    <span th:case="3">qq:1028107280</span> 
</div>

4. each迭代遍历

<table border="1">
    <tr>
       <th>姓名</th>
       <th>QQ号</th>
       <th>微信号</th>
    </tr>
    <tr th:each="info : ${list}">
        <td th:text="${info.username}"></td>
        <td th:text="${info.qq}"></td>
        <td th:text="${info.wechat}"></td>
    </tr>
</table>

总结

Thymeleaf的语法很多,有种像刚学习jsp的感觉,换汤不换药,都是为了展示数据嘛,这里准备了几篇大神们总结的Thymeleaf语法文章,方便大家用的时候查阅使用吧,也方便了本猿,giao~~~

  • https://blog.csdn.net/zrk1000/article/details/72667478
  • https://www.cnblogs.com/itdragon/archive/2018/04/13/8724291.html
  • 官方文档:https://www.thymeleaf.org/documentation.html
  • 源码地址:https://github.com/zheng-weiwei/public/tree/master/springboot-thymeleaf

感谢,请多多指教!

猜你喜欢

转载自blog.csdn.net/weixin_38323872/article/details/98874428