一、前言

SrpingBoot支持如JSPThymeleafFreeMarkerMustacheVelocity等各种模板引擎,同时还为开发者提供了自定义模板扩展的支持。

使用嵌入式Servlet容器时,请避免使用JSP,因为使用JSP打包后会存在一些限制。

SpringBoot使用上述模板,默认从src/main/resources/templates下加载。

二、thymeleaf介绍

Thymeleaf是现代化服务器端的Java模板引擎,不同与其它几种模板的是Thymeleaf的语法更加接近HTML,并且具有很高的扩展性。详细资料可以浏览官网

特点

  • 支持无网络环境下运行,由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。所以它可以让前端小姐姐在浏览器中查看页面的静态效果,又可以让程序员小哥哥在服务端查看带数据的动态页面效果。
  • 开箱即用,为Spring提供方言,可直接套用模板实现JSTL、 OGNL表达式效果,避免每天因套用模板而修改JSTL、 OGNL标签的困扰。同时开发人员可以扩展自定义的方言。
  • SpringBoot官方推荐模板,提供了可选集成模块(spring-boot-starter-thymeleaf),可以快速的实现表单绑定、属性编辑器、国际化等功能。

三、使用

3.1 首先要在pom.xml中添加对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>

3.2 添加完thymeleaf模板依赖的pom.xml文件内容如下

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.kgc</groupId>
<artifactId>springboot1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot1</name>
<description>我用的第一个maven工程</description> <!--版本采用的是最新的 2.0.1.RELEASE
TODO 开发中请记得版本一定要选择 RELEASE ,因为是稳定版本且BUG少-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <!--模块(spring-boot-starter-thymeleaf),可以快速的实现表单绑定、属性编辑器、国际化等功能。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 默认就内嵌了Tomcat 容器,如需要更换容器也极其简单-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试包,当我们使用 mvn package 的时候该包并不会被打入,因为它的生命周期只在 test 之内-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--该依赖可以不添加,但是在 IDEA 和 STS 中不会有属性提示,
没有提示的配置就跟你用记事本写代码一样苦逼,出个问题弄哭你去-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

3.3  创建ThymeleafController.java

创建一个ThymeleafController用来映射HTTP请求与页面的跳转,下面写了两种方式,第一种比较直观和优雅,第二种相对普遍且代码较少,且迎合从struts2跳坑的朋友们…

Spring4.3以后为简化@RequestMapping(method = RequestMethod.XXX)的写法,故而将其做了一层包装,也就是现在的GetMappingPostMappingPutMappingDeleteMappingPatchMapping

 package cn.kgc.controller;
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;
import javax.servlet.http.HttpServletRequest;
/**
* ThymeleafController用来映射HTTP请求与页面的跳转
*/
@Controller
@RequestMapping
public class ThymeleafController {
/**
* 方法1:返回ModelAndView对象,
* 将视图名称封装到该对象的viewName属性中
* 将页面所需要的数据封装到该对象的addObject对象中
* @return
*/
@GetMapping("/index.do")
public ModelAndView index() {
ModelAndView view = new ModelAndView();
// 设置跳转的视图 默认映射到 src/main/resources/templates/{viewName}.html
view.setViewName("index");
// 设置属性
view.addObject("title", "我的第一个ThymeleafWEB页面");
view.addObject("desc", "欢迎进入课工场学习");
Teacher teacher=new Teacher(1,"Holly","964918306","北大青鸟-南京中博软件学院-课工场");
view.addObject("teacher", teacher);
return view;
} @GetMapping("/index1")
public String index1(HttpServletRequest request) {
// TODO 与上面的写法不同,但是结果一致。
// 设置属性
Teacher teacher=new Teacher(1,"Holly","964918306","北大青鸟-南京中博软件学院-课工场");
request.setAttribute("teacher", teacher);
request.setAttribute("title", "我的第一个ThymeleafWEB页面");
request.setAttribute("desc", "欢迎进入课工场学习");
// 返回的 index 默认映射到 src/main/resources/templates/xxxx.html
return "index";
} class Teacher {
private int tid;
private String tname;
private String qq;
private String address; public Teacher() {
} public Teacher(int tid, String tname, String qq, String address) {
this.tid = tid;
this.tname = tname;
this.qq = qq;
this.address = address;
} public int getTid() {
return tid;
} public void setTid(int tid) {
this.tid = tid;
} public String getTname() {
return tname;
} public void setTname(String tname) {
this.tname = tname;
} public String getQq() {
return qq;
} public void setQq(String qq) {
this.qq = qq;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}
}

3.4 创建index.html的模板文件

在src/main/resources/templates目录下创建一个名index.html的模板文件,可以看到thymeleaf是通过在标签中添加额外属性动态绑定数据的

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<!-- 可以看到 thymeleaf 是通过在标签里添加额外属性来绑定动态数据的 -->
<!--/*@thymesVar id="title" type=""*/-->
<title th:text="${title}">Title</title>
<!-- 在/resources/static/js目录下创建一个hello.js 用如下语法依赖即可-->
<script type="text/javascript" th:src="@{/js/hello.js}"></script>
</head>
<body>
<h1 th:text="${desc}">Hello World</h1>
<h2>=====作者信息=====</h2>
<p th:text="${teacher.tid}"></p>
<p th:text="${teacher.tname}"></p>
<p th:text="${teacher.qq}"></p>
<p th:text="${teacher.address}"></p>
</body>
</html>

停一下,在你的机器上看到的是报错的情况,如下图所示,但是页面却可以展示数据,这个我们稍加配置就可以解决错误问题

我们在idea中设置一下就ok

3.5 静态效果:  运行index.html既可以看到如下的静态效果

3.5 动态效果:

在浏览器输入:http://localhost:9090/springboot1/index.do可以看到渲染后的效果,真正意义上的动静分离了

总结:

Thymeleaf参考手册:https://blog.csdn.net/zrk1000/article/details/72667478

参考文章出处:

3.SpringBoot整合Thymeleaf模板的更多相关文章

  1. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  2. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  3. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  4. SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

    在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...

  5. (二)springboot整合thymeleaf模板

    在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...

  6. springboot整合 Thymeleaf模板

    首先引入maven jar依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  7. SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目

    如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的.但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少 ...

  8. springboot整合thymeleaf+tiles示例

    网上关于此框架的配置实在不多,因此想记录下来以防忘记 因为公司框架基于上述(公司采用gradle构建项目,楼主采用的是maven),所以楼主能少走些弯路: 1.创建springboot-maven项目 ...

  9. 三、SpringBoot整合Thymeleaf视图

    目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...

随机推荐

  1. hdu5242 上海邀请赛 优先队列+贪心

    题意是给你一棵树    n个点 n-1条边   起点是1   每一个点都有权值 每次能从根节点走到叶子节点   经行k次游戏 每次都是从1開始    拿过的点的权值不能拿第二次   问最大权值和. 開 ...

  2. redis client 2.0.0 pipeline 的list的rpop bug

    描写叙述: redis client 2.0.0 pipeline 的list的rpop 存在严重bug,rpop list的时候,假设list已经为空的时候,rpop出来的Response依旧不为n ...

  3. Linux vs Window

    目前国内Linux更多的是应用于服务器上,而桌面操作系统更多使用的是Window.主要区别如下: 比较 Windows Linux 界面 界面统一,外壳程序固定所有Windows程序菜单几乎一致,快捷 ...

  4. vim copy termi

    用vim写代码时,经常遇到这样的场景,复制多行,然后粘贴. 这样做:1. 将光标移动到要复制的文本开始的地方,按v进入可视模式.2. 将光标移动到要复制的文本的结束的地方,按y复制.此时vim会自动将 ...

  5. Android系统Recovery工作原理之使用update.zip升级过程分析(六)---Recovery服务流程细节【转】

    本文转载自:http://blog.csdn.net/mu0206mu/article/details/7465439  Android系统Recovery工作原理之使用update.zip升级过程分 ...

  6. 多线程编程和Java网络编程

    1. 线程概述 多任务处理有两种类型:基于进程.基于线程(进程是指一种“自包容”的运行程序,有自己的地址空间; 线程是进程内部单一的一个顺序控制流) 基于进程的特点是允许计算机同时运行两个或更多的程序 ...

  7. Gym - 101981J The 2018 ICPC Asia Nanjing Regional Contest J.Prime Game 计数

    题面 题意:1e6的数组(1<a[i]<1e6),     mul (l,r) =l × (l+1) ×...× r,  fac(l,r) 代表 mul(l,r) 中不同素因子的个数,求s ...

  8. html5小知识点

    1.兼容性问题: 对于不支持H5标签的浏览器,可以使用javascript来解决他们.然后在样式表中对这些标签定义一下默认的display:block. 采用第三方库:html5shiv.js < ...

  9. Python 38 初识数据库

    数据库 1.什么是mysql,什么是数据库? 文件处理就可以将数据永久存储 问题 1.管理不方便 2.文件操作效率问题 3.一个程序不太可能仅运行在同一台电脑上 提高计算机性能的方式 1.垂直扩展  ...

  10. go的基础数据类型

    一.基础数据类型 在go语言中,数据类型用于申明函数和变量 go语言的类型 数据类型 描述 布尔型 布尔型值的只能是true 和 false ,例如 var b bool = true, 布尔型值声明 ...