九、SpringBoot集成Thymeleaf模板引擎
Thymeleaf咋读!???
呵呵,是不是一脸懵逼。。。哥用我的大学四级英文知识告诉你吧:[θaimlif]。
啥玩意?不会音标?...那你就这样叫它吧:“赛母李府”,大部分中国人是听不出破绽的。。。。
Thymeleaf是啥?
四个字:页面模板(有道翻译结果)
就是在页面里面导入一些特有的标签和表达式,帮助我们更好地渲染页面
个人理解:
Thyme:百里香。leaf:叶子;
百里香是啥玩意?
经过上面的观察和分析,我感觉Thymeleaf的创作者是一个很有“生活”的人,我们看到上面图片上的百里香,一片一片的鲜翠的叶子很和谐的长在不同的枝头就好比我们的一个个HTML“叶面”分别存放在不同的文件夹下面,她们虽然在不同的枝头,但是她们的模样都很相似,这些相似点就是模板。。。
好了,哥敢用哥的一包花生米和一瓶啤酒打赌!!!如果你有幸阅读到哥的这篇文章,那么这绝逼是你见过对Thymeleaf最最最牛X的介绍。。。
官方介绍:https://www.thymeleaf.org/
Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。
看了上面的官方解释是不是更是一脸懵逼。。。那就不要纠结了,我们下面直接看她到底怎么用,用来干什么,这样你不就知道Thymeleaf是啥了吗
SpringBoot集成Thymeleaf:
首先为了大家看的不懵逼,我先展开一下整体项目结构:
1、pom.xml 添加 Thymeleaf 模板引擎
<!--thymeleaf模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、application.properties 配置 Thymeleaf 信息
#下面是thymeleaf配置
# 启用缓存:建议生产开启
spring.thymeleaf.cache=false
# 建议模版是否存在
spring.thymeleaf.check-template-location=true
# Content-Type 值
spring.thymeleaf.servlet.content-type=text/html
# 是否启用
spring.thymeleaf.enabled=true
# 模版编码
spring.thymeleaf.encoding=utf-8
# 应该从解析中排除的视图名称列表(用逗号分隔)
spring.thymeleaf.excluded-view-names=
# 模版模式
spring.thymeleaf.mode=HTML5
# 模版存放路径
spring.thymeleaf.prefix=classpath:/templates/
# 模版后缀
spring.thymeleaf.suffix=.html
3、创建controller类,编写代码
我们创建ThymeleafTest.java,代码如下:
@Controller
@RequestMapping("/thymeleafTest")
public class ThymeleafTest {
@RequestMapping("/testThymeleaf")
public ModelAndView testThymeleaf(){
ModelAndView modelAndView=new ModelAndView("/thymeleaf");
modelAndView.addObject("user","任正非");
return modelAndView;
}
}
4、 创建模板,编写html代码
我们在resources/templates下创建index.html,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>thymeleaf测试页面</title>
</head>
<body>
<h1 th:text="${user}"></h1>
</body>
</html>
启动项目然后访问:
至此thymeleaf就成功集成了,是不是很开心。。呵呵,但是你别高兴的太早了,,,此时你会发现你之前配置成功的JSP页面现在不能访问了
关于这个问题的解决请参考鄙人的另一篇博客:
https://www.cnblogs.com/luzhanshi/p/10925851.html
九、SpringBoot集成Thymeleaf模板引擎的更多相关文章
- SpringBoot集成Thymeleaf模板引擎
简单介绍 目前在JavaEE领域有几中比较常用的模板引擎,分别是Jsp.Velocity.Freemarker.Thymeleaf,对Freemark语法不是特别熟悉,不过对于前端页面渲染效率来说,j ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- SpringBoot使用thymeleaf模板引擎
(1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- SpringBoot日记——Thymeleaf模板引擎篇
开发通常我们都会使用模板引擎,比如:JSP.Velocity.Freemarker.Thymeleaf等等很多,那么模板引擎是干嘛用的? 模板引擎,顾名思义,是一款模板,模板中可以动态的写入一些参数, ...
- SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图
在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- Springboot与Thymeleaf模板引擎整合基础教程(附源码)
前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...
- SpringBoot使用thymeleaf模板引擎引起的模板视图解析错误
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as ...
随机推荐
- node.js--使用Express中app.use()分模块开发
app.use(path,callback)中的callback既可以是router对象又可以是函数:将一个URL路径与一个函数绑定,第一个参数为访问的路径,如果第一参数为空,则表示任何路径都触发这个 ...
- Deadlock_synchromized-Java_se
class Test implements Runnable{ private boolean flag; Test(boolean flag) { this.flag = flag; } publi ...
- 优雅的swiper实例
swiper实例 <!DOCTYPE html> <html lang="en"> <head> <meta name="vie ...
- idea启动,mysql连接超时错误
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received ...
- Linux往log中写日志
void writelog(const char* log) { time_t tDate; struct tm* eventTime; time(&tDate);//得到系统当前时间 //将 ...
- C++质因数分解
// CPP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include<cs ...
- Codeforces1203F2. Complete the Projects (hard version) (贪心+贪心+01背包)
题目链接:传送门 思路: 对于对rating有提升的项目,肯定做越多越好,所以把$b_{i} >= 0$的项目按rating要求从小到大贪心地都做掉,得到最高的rating记为r. 对于剩余的$ ...
- Vue基础第一章
Vue的简单示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- strcmp-sse2-unaligned.S: No such file or directory.
__strcmp_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S:3131 ../sysdeps/x8 ...
- Linux网络性能优化方法简析
Linux网络性能优化方法简析 2010-12-20 10:56 赵军 IBMDW 字号:T | T 性能问题永远是永恒的主题之一,而Linux在网络性能方面的优势则显而易见,这篇文章是对于Linux ...