26、模板引擎thymeleaf

简介: 模板引擎根据一定的语义,将数据填充到模板中,产生最终的HTML页面。模板引擎主要分两种:客户端引擎和服务端引擎。

相对html+js的传统设计,现在很多网站都采用div&css+标签化+模块化的设计。模板引擎根据一定的语义,将数据填充到模板中,产生最终的HTML页面。


模板引擎主要分两种:客户端引擎和服务端引擎。


客户端渲染:


模板和数据分别传送到客户端,在客户端由JavaScript模板引擎渲染出最终的HTML视图。将模板渲染放置在客户端做,可以降低服务端的压力,并且如果前端内容分别来自多个后台系统,而这些后台的架构各不相同(Java、.NET、Ruby等),则服务器端渲染需要采用不同的技术,模板资源无法共享。

 

服务端渲染:


引擎在服务器端将模板和数据合成,返回最终的html页面,相对于客户端渲染,数据存储更加安全。主要有freemarker、velocity、thymeleaf等。


微信图片_20220501215958.png


相较与其他的模板引擎,thymeleaf它有如下三个特点:


(a) 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,同时也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。


(b) 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。


(c) 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

 

1、 新建项目sc-thymeleaf,对应的pom.xml文件如下


<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>spring-cloud</groupId>
<artifactId>sc-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-thymeleaf</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>


2、 新建springboot启动类


package sc.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ThymeleafApplication {
public static void main(String[] args) {
SpringApplication.run(ThymeleafApplication.class, args);
}
}


3、 新建配置文件application.yml


server:
  port: 8090
spring:
  application:
    name: sc-thymeleaf
  thymeleaf:
    cache: false


说明:thymeleaf所有的配置项可以参考类


org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties


常用配置说明:


# THYMELEAF (ThymeleafAutoConfiguration)

#开启模板缓存(默认值:true)

spring.thymeleaf.cache=true

#Check that the template exists before rendering it.

spring.thymeleaf.check-template=true

#检查模板位置是否正确(默认值:true)

spring.thymeleaf.check-template-location=true

#Content-Type的值(默认值:text/html)

spring.thymeleaf.content-type=text/html

#开启MVC Thymeleaf视图解析(默认值:true)

spring.thymeleaf.enabled=true

#模板编码

spring.thymeleaf.encoding=UTF-8

#要被排除在解析之外的视图名称列表,用逗号分隔

spring.thymeleaf.excluded-view-names=

#要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)

spring.thymeleaf.mode=HTML5

#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)

spring.thymeleaf.prefix=classpath:/templates/

#在构建URL时添加到视图名称后的后缀(默认值:.html)

spring.thymeleaf.suffix=.html

#Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。

spring.thymeleaf.template-resolver-order=

#可解析的视图名称列表,用逗号分隔

spring.thymeleaf.view-names=

 

其实完全可以使用不用配置,但是Spring Boot官方文档建议在开发时将缓存关闭,默认为true


微信图片_20220501220009.png


4、 新建Controller


package sc.thymeleaf.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import sc.thymeleaf.model.User;
@Controller
public class UserController {
@RequestMapping("/user/list")
public String userList2(Model model) throws Exception {
model.addAttribute("hello", "Hello, thymeleaf!");
List<User> list = new ArrayList<User>();
User u1 = new User();
u1.setId(1);
u1.setName("huangjinjin");
u1.setAge(30);
u1.setPosition("cto");
list.add(u1);
User u2 = new User();
u2.setId(2);
u2.setName("huang ge");
u2.setAge(32);
u2.setPosition("cco");
list.add(u2);
model.addAttribute("list", list);
return "/user/list";
}
}


5、 新建模板文件


微信图片_20220501220015.png


说明:Thymeleaf默认模板路径在classpath:/templates/下


微信图片_20220501220018.png


6、 运行ThymeleafApplication.java类,启动项目


微信图片_20220501220022.png


7、 在浏览器输入http://127.0.0.1:8090/user/list


微信图片_20220501220026.png


这里不深入讲解Thymeleaf模板引擎的语法,如果想学习Thymeleaf的基本语法可以参考https://www.cnblogs.com/ityouknow/p/5833560.html或者自行找些资料学习

 

https://gitee.com/hjj520/spring-cloud-2.x





相关文章
|
数据安全/隐私保护
【鸿蒙软件开发】文本输入(TextInput/TextArea)
【鸿蒙软件开发】文本输入(TextInput/TextArea)
1923 0
|
SQL 前端开发 数据可视化
MySQL Workbench 使用教程 - 如何使用 Workbench 操作 MySQL / MariaDB 数据库中文指南
MySQL Workbench 是一款专门为 MySQL 设计的可视化数据库管理软件,我们可以在自己的计算机上,使用图形化界面远程管理 MySQL 数据库。有关 MySQL 远程管理软件,你可以选择 Windows 下的 HeidiSQL,MacOS 下的 Sequel Ace 或者 MySQL 官方推出的跨平台客户端 MySQL Workbench 。
12788 0
|
9月前
|
存储 人工智能 Linux
OpenClaw部署与优化保姆级教程:1分钟阿里云/本地配置百炼+claude-mem+OpenViking Skill,Token 成本降96%
OpenClaw(俗称大龙虾,原Clawdbot、Moltbot)作为一款高性能AI Agent框架,凭借自然语言驱动的任务自动化能力,成为代码开发、流程协作中的重要工具,但在长周期任务执行中,其无状态特性带来的记忆短板与Token高消耗问题,成为企业级落地的核心阻碍。2026年最新技术实践中,通过集成claude-mem与OpenViking两款开源项目,可从底层重构OpenClaw的记忆管理体系,实现Token成本断崖式下降96%,同时结合阿里云与本地多平台部署方案,能让零基础用户快速搭建起高性价比的AI Agent运行环境。本文将深度拆解两款开源项目的核心优化逻辑,同时提供阿里云、Mac
2795 18
|
SQL 存储
milvus的attu里查询SQL如何编写
【6月更文挑战第4天】milvus的attu里查询SQL如何编写
1799 4
|
关系型数据库 MySQL 调度
DataX教程(05)- DataX Web项目实践
DataX教程(05)- DataX Web项目实践
4796 0
|
JavaScript 前端开发 应用服务中间件
【Vue面试题三十】、vue项目本地开发完成后部署到服务器后报404是什么原因呢?
这篇文章分析了Vue项目在服务器部署后出现404错误的原因,主要是由于history路由模式下服务器缺少对单页应用的支持,并提供了通过修改nginx配置使用`try_files`指令重定向所有请求到`index.html`的解决方案。
【Vue面试题三十】、vue项目本地开发完成后部署到服务器后报404是什么原因呢?
|
12月前
|
存储 负载均衡 Java
SpringCloud框架
Nacos实现服务注册与发现,支持心跳检测、负载均衡及动态服务列表更新。其分级存储模型通过命名空间、组、DataId实现多维度配置管理。相比Eureka,Nacos具备更优的实时性与配置中心能力。OpenFeign整合负载均衡,Sentinel提供限流熔断,Gateway则实现路由、过滤与鉴权,构建稳定高效微服务体系。
219 0
|
移动开发 前端开发
基于Jeecg-boot的flowable流程支持拒绝同意流程操作(二)
基于Jeecg-boot的flowable流程支持拒绝同意流程操作(二)
680 0
|
人工智能 自然语言处理 安全
Claude 4 隆重登场!claude 4国内怎么使用才正确?一分钟学会!
Anthropic 日前正式推出其新一代旗舰模型
1431 4
|
Web App开发 JavaScript 前端开发
解决Vue.js Devtools未检测到Vue实例的问题
解决Vue.js Devtools未检测到Vue实例的问题
2247 1