当前位置: 首页 > news >正文

如何优雅的将Thymeleaf集成到SpringBoot

1. 什么是Thymeleaf?

Thymeleaf是用于Web和独立环境的现代服务器端Java模板引擎。Thymeleaf的主要目标是将优雅的自然模板带到您的开发工作流程中—HTML能够在浏览器中正确显示,并且可以作为静态原型,从而在开发团队中实现更强大的协作。Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。

在使用Thymeleaf时,可以使用以下五种标准表达式:

${...} : 变量表达式。
*{...} : 选择表达式。
#{...} : 消息 (i18n) 表达式。
@{...} : 链接 (URL) 表达式。
~{...} : 片段表达式。

现在你可能看不太懂这几个表达式想要表达的意思,但是通过后面的实例相信能很容易的理解这些表达式的意思。

2. 使用SpringBoot集成Thymeleaf

首先创建一个SpringBoot项目,项目的结构如下:

java文件夹中存放所有的java代码

static文件夹中存放所需要的一些静态资源

templates中用来存放html代码

在pom.xml中导入Thymeleaf相关的依赖

<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
</dependency>

导入依赖后就可以使用Thymeleaf了,在templates下创建一个index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"><!--引入thymeleaf-->
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p th:text="${msg}">你好</p>
</body>
</html>

在Controller包下新建一个indexController.java

@Controller
public class indexController {
    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("msg","集成thymeleaf");
        return "index";
    }
}

启动SpringBoot,在浏览器中输入 http://localhost:8080/index

接下来就开始正式学习thymeleaf的相关内容

2.1 Thymeleaf变量表达式

${...} : 变量表达式。

我们可以通过${...}这样的语法在html文件内获取变量。依旧以学生为实例,在entity包下新建student.java

public class Student {
    private int id;
    private String name;
    private String age;
    public Student(int id,String name,String age){
        this.id=id;
        this.name=name;
        this.age=age;
    }
    //省略getter and setter方法
}

继续新建一个studentController

@Controller
public class studentController {
    @GetMapping("/student")
    public String student(Model model){
        Student student=new Student(1,"少掉下巴","22");
        model.addAttribute("student",student);
        return "student";
    }
}

再新建一个student.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"><!--引入thymeleaf-->
<head>
    <meta charset="UTF-8">
    <title>student information</title>
</head>
<body>
<h1 th:text="${student.id}"/>
<h1 th:text="${student.name}"/>
<h1 th:text="${student.age}"/>
</body>
</html>

通过运行结果观察到Thymeleaf中的变量表达式使用${变量名}的方式获取其中的数据

2.2 thymeleaf选择表达式

*{...} : 选择表达式。

选择表达式的用法和变量表达式相似,只不过选择表达式先使用th:object选择变量,再用*{属性}取值

修改上面的student.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"><!--引入thymeleaf-->
<head>
    <meta charset="UTF-8">
    <title>student information</title>
</head>
<body>
<h1>使用变量表达式</h1>
<h2 th:text="${student.id}"/>
<h2 th:text="${student.name}"/>
<h2 th:text="${student.age}"/>
<h1>使用选择表达式</h1>
<div th:object="${student}">
    <h2 th:text="*{id}"/>
    <h2 th:text="*{name}"/>
    <h2 th:text="*{age}"/>
</div>
</body>
</html>

两者所达到的效果是一样的。

2.3 thymeleaf消息表达式

#{...} : 消息 (i18n) 表达式。

消息表达式(通常称为文本外部化,国际化或i18n)允许从外部源(如:.properties)文件中检索,通过键值对的方式引用

首先创建一个properties文件

在application.properties中设置messages的路径:

spring.messages.basename=i8bn

在i8bn.properties中设置键值对

hello=hello

继续在student.html中使用:

<h1>使用消息表达式</h1> <h2 th:text="#{hello}"/>

即可以引用到外部文件的内容。

2.4 thymeleaf链接表达式

@{...} : 链接 (URL) 表达式。

从名字上就能看出链接表达式是设置链接时最常用的表达式,一般和th:action和th:href配合使用

在上述的student.html中继续增加以下内容

<a href="index.html" th:href="@{'http://localhost:8080/student?name='+${student.name}}">index</a>
<a href="index.html" th:href="@{'student?name='+${student.name}}">index2</a>
<a href="index.html" th:href="@{'/student?name='+${student.name}}">index3</a>

通过链接的方式可以在链接后增加指定参数,方便request调用

2.5 thymeleaf片段表达式

~{...} : 片段表达式。

片段表达式是一种简单的方法用来表示标记的片段并将其移动到模板中,一般和th:insert、th:replace搭配使用,我们实际来操作一下:

我在hello.html中写两个div

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"><!--引入thymeleaf-->
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:fragment="header">
    <h3>头部</h3>
</div>
<!--尾部-->
<div th:fragment="footer" id="footer">
    <h3>尾部</h3>
</div>
</body>
</html>

然后在student.html中用片段表达式来引用它

<div th:replace="hello :: html"></div>

写在hello.html中的内容就被引用过来了。

3. 总结

对后端人员来说,虽然不用精通前端知识,但是必要的前端知识还是掌握一些最好。Thymeleaf是个很强大的框架,这篇文章也只是对其中一部分做了介绍,作为后端程序员来说,用到的时候可以通过查询各种资料代码来使用,不需要深入了解,如果想要深入学习,官方文档是最好的学习资料。

相关文章:

  • 开发基于SpringBoot和BootStrap的全栈论坛网站(一):准备阶段
  • 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(三)——使用Flask-Login库实现登录功能...
  • 开发基于SpringBoot和BootStrap的全栈论坛网站(二):后端人员如何快速使用BootStrap
  • java虚拟机(三)java的垃圾回收机制详解
  • 查看ORACLE的实际执行计划
  • 开发基于SpringBoot和BootStrap的全栈论坛网站(三):登陆注册以及cookies的功能完成
  • 开发基于SpringBoot和BootStrap的全栈论坛网站(四):完成问题发布功能
  • OSChina 周日乱弹 ——冯小牛 我要治愈你!
  • 开发基于SpringBoot和BootStrap的全栈论坛网站(五):完成首页展示以及分页功能
  • 冒泡排序法与二分查找法
  • github回退版本时本地代码被覆盖(已解决)
  • CentOS 6.5系统上安装SVN服务器端的方法及目录访问权限配置(转总结)
  • 开发基于SpringBoot和BootStrap的全栈论坛网站(六):完成个人中心、问题详情和问题编辑
  • 开发基于SpringBoot和BootStrap的全栈论坛网站(七):完成回复和二级回复功能
  • 项目管理过程 工作绩效数据,信息和报告
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • [LeetCode] Wiggle Sort
  • [译] 怎样写一个基础的编译器
  • 03Go 类型总结
  • 4个实用的微服务测试策略
  • Android Studio:GIT提交项目到远程仓库
  • echarts花样作死的坑
  • Java 网络编程(2):UDP 的使用
  • Java超时控制的实现
  • java中具有继承关系的类及其对象初始化顺序
  • Js基础——数据类型之Null和Undefined
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • Python十分钟制作属于你自己的个性logo
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 对超线程几个不同角度的解释
  • 复习Javascript专题(四):js中的深浅拷贝
  • 技术:超级实用的电脑小技巧
  • 经典排序算法及其 Java 实现
  • 你真的知道 == 和 equals 的区别吗?
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 微服务入门【系列视频课程】
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • ​一、什么是射频识别?二、射频识别系统组成及工作原理三、射频识别系统分类四、RFID与物联网​
  • #define用法
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (zhuan) 一些RL的文献(及笔记)
  • (二)构建dubbo分布式平台-平台功能导图
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (五)IO流之ByteArrayInput/OutputStream
  • (转)EOS中账户、钱包和密钥的关系
  • (转)mysql使用Navicat 导出和导入数据库
  • (转)ORM
  • (转)shell调试方法