Web开发:Thymeleaf模板引擎

devtools/2024/10/10 14:14:12/
htmledit_views">

1.1 html" title=Thymeleaf>Thymeleaf简介

1.1.1 模板引擎

模板引擎是为了使用户界面与业务数据分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。

1.1.2 html" title=Thymeleaf>Thymeleaf介绍

        html" title=Thymeleaf>Thymeleaf是一个流行的现代服务器端Java模板引擎,它专门设计用于Web和独立环境中的应用程序。它允许开发者以清晰和直观的方式将服务器端的数据与HTML、XML、JavaScript、CSS以及纯文本等模板文件结合起来。html" title=Thymeleaf>Thymeleaf的最大特点之一是它的“自然模板”技术,这意味着开发者可以编写标准的HTML代码,并通过html" title=Thymeleaf>Thymeleaf特有的属性和表达式(如th:textth:if等)来动态插入或修改内容,而无需改变HTML的结构或引入特定的模板语法。

html" title=Thymeleaf>Thymeleaf与Spring MVC等Java Web框架集成得非常好,是Spring Boot推荐的默认模板引擎之一。它提供了丰富的功能,包括数据绑定、条件语句、循环、国际化支持、模板布局等,使得开发者可以轻松地构建出动态、响应式且易于维护的Web界面。

        html" title=Thymeleaf>Thymeleaf的性能也非常出色,它能够在短时间内处理并渲染大量的模板数据,从而满足高并发Web应用的需求。此外,html" title=Thymeleaf>Thymeleaf还具有良好的扩展性和可定制性,允许开发者根据需要进行配置和优化。

        总的来说,html" title=Thymeleaf>Thymeleaf是一个功能强大、易于使用且高度灵活的模板引擎,它为Java Web开发者提供了一种高效且优雅的方式来处理模板渲染和数据展示。

1.1.3 html" title=Thymeleaf>Thymeleaf特点

html" title=Thymeleaf>Thymeleaf是一个跟Velocity、FreeMarker类似的模板引擎,它可以完全代替JSP。
与其他的模板引擎相比较,它又如下极吸引人的特点。

  • html" title=Thymeleaf>Thymeleaf的模板语法设计得非常接近HTML,它允许开发者编写几乎无差异的HTML代码,并通过添加html" title=Thymeleaf>Thymeleaf特有的属性和表达式(如th:text${...}等)来动态插入数据或控制页面元素。这种设计保持了HTML的清晰和可读性,降低了模板的复杂度。
  • 与其他模板引擎相比,如JSP或Velocity,html" title=Thymeleaf>Thymeleaf的自然模板技术使得html" title=前端>前端设计师和开发者能够更容易地协同工作。设计师可以直接使用和设计标准的HTML模板,而无需担心模板引擎的特定语法会破坏HTML结构或增加学习成本。
  • html" title=Thymeleaf>Thymeleaf能够轻松集成到Spring框架中,特别是与Spring MVC的结合使用,为开发者提供了更加灵活和强大的Web开发能力。它支持Spring的表达式语言(SpEL),使得数据绑定、表单验证、国际化等功能变得更加简便。
  • 相比其他模板引擎,html" title=Thymeleaf>Thymeleaf与Spring框架的集成更为紧密。在Spring Boot项目中,html" title=Thymeleaf>Thymeleaf甚至被作为默认的模板引擎之一,进一步简化了配置和使用过程。这使得基于Spring框架的Web应用开发更加高效和便捷。
  • html" title=Thymeleaf>Thymeleaf在提供丰富功能的同时,也注重性能优化。它采用高效的模板解析和执行机制,能够快速处理并渲染大量数据,满足高并发Web应用的需求。此外,html" title=Thymeleaf>Thymeleaf还提供了自动的HTML转义功能,防止跨站脚本攻击(XSS)等安全问题。

1.2 快速上手

1.构建项目

 

 2.添加依赖
html"><?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><!-- 在此处将3.3.4版本修改成3.3.3版本 --><version>3.3.3</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>com.bdqn</groupId><artifactId>html" title=Thymeleaf>Thymeleaf</artifactId><version>0.0.1-SNAPSHOT</version><name>html" title=Thymeleaf>Thymeleaf</name><description>html" title=Thymeleaf>Thymeleaf</description><properties><java.version>21</java.version></properties><dependencies><!-- 引入SpringBoot的Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId><version>6.3.3</version></dependency><!-- 引入JSP引擎依赖,SpringBoot内置Tomcat没有此依赖 --><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.7</version></dependency><!-- 引入JSTL标签库依赖 --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- 引入MyBatis-Plus支持(不需要再引入MyBatis包) --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.7</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>3.0.3</version></dependency><!-- 引入Druid数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-3-starter</artifactId><version>1.2.20</version></dependency><!-- &lt;!&ndash; 引入MyBatis-Plus动态数据源支持 &ndash;&gt; --><!-- <dependency> --><!--     <groupId>com.baomidou</groupId> --><!--     <artifactId>dynamic-datasource-spring-boot3-starter</artifactId> --><!--     <version>4.1.2</version> --><!-- </dependency> --><!-- 引入MySQL驱动 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!-- 引入Lombok完成实体类自动Getter/Setter --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- 引入Slf4j日志依赖 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></dependency><!-- 引入SpringBoot测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.4.1</version></dependency><!-- 配置热部署 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies><build><resources><resource><directory>src/main/resources</directory><filtering>true</filtering><excludes><exclude>application-dev.properties</exclude><exclude>application-pro.properties</exclude><exclude>application-test.properties</exclude></excludes></resource><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>application.yml</include></includes></resource></resources><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><fork>true</fork><exclude><groupId>org.project-lombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
  • spring-boot-starter-data-jpa:Spring Boot DataJPA 启动器
  • spring-boot-starter-thymeleaf:Spring Boot html" title=Thymeleaf>Thymeleaf 模板引擎启动器
3.application.yml添加配置
html">spring:#html" title=Thymeleaf>Thymeleaf配置thymeleaf:cache: false #开启模板缓存(默认值:true)
4.Model准备
html">public interface UserService {User login(String usrName, String usrPassword);int addUser(User user);int deleteUser(Long usrId);int updateUser(User user);User getUser(Long usrID);List<User> findAllUsers();}
5.控制器开发
@Controller
public class ExampleController {@Resourceprivate UserService userService;@GetMapping("/hello/{id}")public String getUser(@PathVariable("id")Long usrId, Model model){User user = userService.getUser(usrId);model.addAttribute("user",user);return "demo/hello";}
6.页面开发
html"><!DOCTYPE html>
<!--加入对html" title=Thymeleaf>Thymeleaf模板引擎支持-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Hello</title><link rel="stylesheet" th:href="@{/css/index.css}" />
</head>
<body>
欢迎您,<span th:text="${user.usrName}">张三</span>!
</body>
</html>
7.运行访问

启动项目后在浏览器中输入网址:http://localhost:8080/hello/2

1.3 核心语法

1.3.1 表达式

html">    赋值,<span th:text="${userName}">userName</span>!<br>拼接:<span th:text="'Hello '+ ${userName} + ' ! '"></span><br>拼接:<span th:text="|Hello ${userName}! |">Hello userName!</span><br><p th:text="${test}"></p><p th:text="${session.test}"></p><p th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}"></p><p th:text="${#dates.createToday()}"></p><div th:object="${user}"><span th:text="*{usrId}"></span><span th:text="*{usrName}"></span></div>
    @GetMapping("/string")public String getString(Model model, HttpServletRequest request){User user = userService.getById(1);model.addAttribute("user",user);model.addAttribute("userName","李哈哈");request.setAttribute("test","request");request.getSession().setAttribute("test","session");model.addAttribute("date",new Date());return "demo/string";}
html"><script th:inline="javascript">var msg = 'Hello'+ [[${userName}]] + '!';alert(msg)
</script>
    @GetMapping("/inline")public String inline(Model model){model.addAttribute("userName","李哈哈");return "demo/inline";}

http://www.ppmy.cn/devtools/114806.html

相关文章

sqlgun新闻管理系统

一&#xff0c;打开主页 1.输入框测试回显点 -1union select 1,2,3# 出现回显点2 2.查看数据库表名 -1union select 1,database(),3# 3.查看表名 -1union select 1,2,group_concat(table_name) from information_schema.tables where table_schemasqlgunnews# 4.查看admin中…

网站后缀名学习

目的 学习并记录一些常见的网站后缀名 常见网站后缀名 1、com commercial翻译为商业的。 最常见的域名后缀&#xff0c;全世界范围使用最多的域名&#xff0c;原用于商业组织&#xff0c;现在个人也能注册。 2、net network翻译为网络&#xff0c;更多是专业技术类的网址…

力扣题解(统计特殊整数)

2376. 统计特殊整数 已解答 困难 相关标签 相关企业 提示 如果一个正整数每一个数位都是 互不相同 的&#xff0c;我们称它是 特殊整数 。 给你一个 正 整数 n &#xff0c;请你返回区间 [1, n] 之间特殊整数的数目。 思路&#xff1a; 若n的位数是k&#xff0c;则对于…

uni-app 应用名称 跟随系统语言 改变

官方已确认BUG:&#xff1a;https://ask.dcloud.net.cn/question/164804 { "name" : "%app.name%",//这里随便写&#xff0c;配置了 locales,name 就不生效了 "appid" : "", "description" : "", "versi…

字符串的KMP算法详解及C/C++代码实现

1. 原由 紧接上文&#xff0c;我们知道了暴力匹配的算法在时间运行上的缺陷&#xff0c;假设字符串T的长度为n&#xff0c;字符串P的长度为m&#xff0c;则整个算法的时间复杂度为O( n * m )&#xff0c;而对于一个复杂的现实情况而言 n >> m >> 2 &#xff08;即…

git命令大全

简介&#xff1a;个人学习分享&#xff0c;如有错误&#xff0c;欢迎批评指正 一、Git操作流程 1、代码提交和同步代码 第零步: 工作区与仓库保持一致第一步: 文件增删改&#xff0c;变为已修改状态第二步: git add &#xff0c;变为已暂存状态 $ git status $ git add --al…

PointNet++改进策略 :模块改进 | EdgeConv | DGCNN, 动态图卷积在3d任务上应用

目录 介绍核心思想及其实现核心思想实现步骤 如何改进PointNet**局部几何结构的处理****动态图的引入****特征聚合的灵活性****全局和局部特征的结合** 论文题目&#xff1a;Dynamic Graph CNN for Learning on Point Clouds发布期刊&#xff1a;TOG作者单位&#xff1a;麻省理…

【CPP】类与继承

14 类与继承 在前面我们提到过继承的一些概念,现在我们来回顾一下 打个比方:在CS2中我们把玩家定义为一个类 class 玩家: 血量:100阵营(未分配)服饰(未分配)位置(未分配)武器(未分配)是否允许携带C4(未分配)是否拥有C4(未分配) 当对局创建时,会新生成两个类,这两个类继承自&qu…