广告
返回顶部
首页 > 资讯 > 前端开发 > html >Thymeleaf模板引擎怎么使用
  • 156
分享到

Thymeleaf模板引擎怎么使用

2024-04-02 19:04:59 156人浏览 泡泡鱼
摘要

今天小编给大家分享一下Thymeleaf模板引擎怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来

今天小编给大家分享一下Thymeleaf模板引擎怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

一、模板引擎

jsP、Velocity、Freemarker、Thymeleaf

Thymeleaf模板引擎怎么使用

二、SpringBoot推荐使用Thymeleaf模板引擎

特点:语法更简单,功能更强大;

1、引入Thymeleaf

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2、Thymeleaf的使用

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

    private static final Charset DEFAULT_ENcoding = Charset.forName("UTF-8");

    private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

    public static final String DEFAULT_PREFIX = "classpath:/templates/";

    public static final String DEFAULT_SUFFIX = ".html";
只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

3、导入thymeleaf的名称空间

<html lang="en" xmlns:th="Http://www.thymeleaf.org">

4、使用thymeleaf语法

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>成功!</h2>
    <!--th:text 将div里面的文本内容设置为 -->
    <div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>

三、语法规则

1、th:text;改变当前元素里面的文本内容;?th:任意html属性;来替换原生属性的值

Thymeleaf模板引擎怎么使用

2、表达式

Simple expressions:(表达式语法)
    Variable Expressions: ${...}:获取变量值;OGNL;
            1)、获取对象的属性、调用方法
            2)、使用内置的基本对象:
                #ctx : the context object.
                #vars: the context variables.
                #locale : the context locale.
                #request : (only in WEB Contexts) the httpservletRequest object.
                #response : (only in Web Contexts) the HttpServletResponse object.
                #session : (only in Web Contexts) the HttpSession object.
                #servletContext : (only in Web Contexts) the ServletContext object.
                
                ${session.foo}
            3)、内置的一些工具对象:
#execInfo : infORMation about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{&hellip;} syntax.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analoGous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).

    Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
        补充:配合 th:object="${session.user}:
   <div th:object="${session.user}">
    <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
    <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
    <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
    </div>
    
    Message Expressions: #{...}:获取国际化内容
    Link URL Expressions: @{...}:定义URL;
            @{/order/process(execId=${execId},execType='FAST')}
    Fragment Expressions: ~{...}:片段引用表达式
            <div th:insert="~{commons :: main}">...</div>
            
Literals(字面量)
      Text literals: 'one text' , 'Another one!' ,&hellip;
      Number literals: 0 , 34 , 3.0 , 12.3 ,&hellip;
      Boolean literals: true , false
      Null literal: null
      Literal tokens: one , sometext , main ,&hellip;
Text operations:(文本操作)
    String concatenation: +
    Literal substitutions: |The name is ${name}|
Arithmetic operations:(数学运算)
    Binary operators: + , - , * , / , %
    Minus sign (unary operator): -
Boolean operations:(布尔运算)
    Binary operators: and , or
    Boolean negation (unary operator): ! , not
Comparisons and equality:(比较运算)
    Comparators: > , < , >= , <= ( gt , lt , ge , le )
    Equality operators: == , != ( eq , ne )
Conditional operators:条件运算(三元运算符)
    If-then: (if) ? (then)
    If-then-else: (if) ? (then) : (else)
    Default: (value) ?: (defaultvalue)
Special tokens:
    No-Operation: _

以上就是“Thymeleaf模板引擎怎么使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网html频道。

--结束END--

本文标题: Thymeleaf模板引擎怎么使用

本文链接: https://www.lsjlt.com/news/88580.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Thymeleaf模板引擎怎么使用
    今天小编给大家分享一下Thymeleaf模板引擎怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来...
    99+
    2022-10-19
  • spring Boot怎么与Thymeleaf模板引擎结合使用
    这篇文章给大家介绍spring Boot怎么与Thymeleaf模板引擎结合使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Thymeleaf:Thymeleaf是一个java类库,他是一个xml/xhtml/htm...
    99+
    2023-05-31
    springboot thymeleaf
  • 如何使用Spring Boot thymeleaf模板引擎
    本篇内容主要讲解“如何使用Spring Boot thymeleaf模板引擎”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Spring Boot thymeleaf模板引擎”吧!在早期开...
    99+
    2023-06-07
  • 解读thymeleaf模板引擎中th:if的使用
    目录thymeleaf模板引擎中th:if的使用th:if 条件判断th:if 判断表达式Thymeleaf模板引擎语法使用1、模板引擎thymeleaf使用2、ognl表达式的语法...
    99+
    2022-11-13
    thymeleaf模板引擎 th:if的使用 thymeleaf模板
  • SpringBoot自带模板引擎Thymeleaf使用详解②
    目录 一、条件判断和迭代遍历 1.1 条件判断 2.2 迭代遍历 二、获取域中的数据和URL写法 2.1 获取域中的数据 2.2 URL写法 三、相关配置 一、条件判断和迭代遍历 1.1 条件判断 语法 作用 th:if 条件判断 准...
    99+
    2023-10-21
    spring boot 后端 java thymeleaf 原力计划
  • SpringBoot超详细讲解Thymeleaf模板引擎
    Jsp是最早的模板技术,用来处理视图层的,用来做数据显示的模板 B S结构: B:浏览器:用来显示数据,发送请求,没有处理能力 发送一个请求,访问a.jsp,a.jsp在服务器端变...
    99+
    2022-11-13
  • Java SpringBoot模板引擎之 Thymeleaf入门详解
    目录模板引擎简介引入Thymeleaf模板引擎分析Thymeleaf模板引擎测试Thymeleaf模板引擎1、编写一个TestController2、编写一个测试页面 test.ht...
    99+
    2022-11-12
  • springboot中Thymeleaf模板引擎的示例分析
    这篇文章给大家分享的是有关springboot中Thymeleaf模板引擎的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。模板引擎springboot我们目前是以jar包的形式打包,实际上我们之前是打成w...
    99+
    2023-06-29
  • Nodejs中怎么使用模板引擎以及使用模板引擎渲染HTML
    这篇文章给大家分享的是有关Nodejs中怎么使用模板引擎以及使用模板引擎渲染HTML的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。使用readdir获取指定路径下的所有文件名文件...
    99+
    2022-10-19
  • springboot学习之Thymeleaf模板引擎及原理介绍
    目录模板引擎什么是模板引擎?模板引擎的原理引入ThymeleafThymeleaf分析Thymeleaf 语法学习模板引擎 springboot我们目前是以jar包的形式打包,实际上...
    99+
    2022-11-13
  • C#中Razor模板引擎怎么使用
    这篇文章主要讲解了“C#中Razor模板引擎怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#中Razor模板引擎怎么使用”吧!使用视图引擎可以完成一些需要定制化内容格式的问题,比如...
    99+
    2023-06-29
  • SpringBoot+Thymeleaf基于HTML5现代模板引擎的示例分析
    这篇文章主要介绍了SpringBoot+Thymeleaf基于HTML5现代模板引擎的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。开始使用1.引入依赖SpringB...
    99+
    2023-05-31
    springboot thymeleaf html5
  • Thinkphp 6 使用thinkTemplate 模板引擎
    使用thinkTemplate 模板引擎 由于Thinkphp 5.1 之前的版本 已经将Think-view 拓展 集成到 vendor 中 Tp6 将大部分转为拓展使用 新版框架默认只能支持PHP...
    99+
    2023-09-03
    php 开发语言
  • Thymeleaf模板片断技术怎么使用
    本篇内容主要讲解“Thymeleaf模板片断技术怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Thymeleaf模板片断技术怎么使用”吧! 系统中的很...
    99+
    2022-10-19
  • PHP模板引擎Prototype怎么配置
    这篇文章主要为大家展示了“PHP模板引擎Prototype怎么配置”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“PHP模板引擎Prototype怎么配置”这篇文...
    99+
    2022-10-19
  • jquery有什么模板引擎
    这篇文章主要讲解了“jquery有什么模板引擎”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“jquery有什么模板引擎”吧! j...
    99+
    2022-10-19
  • 怎么用JavaScript实现一个模板引擎
    这篇文章给大家分享的是有关怎么用JavaScript实现一个模板引擎的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。功能分析一个模板引擎,在我看来,就是由两块核心功能组成,一个是用...
    99+
    2022-10-19
  • 怎么用JavaScript写一款EJS模板引擎
    本篇内容介绍了“怎么用JavaScript写一款EJS模板引擎”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1. 起因部门最近的一次分享中,...
    99+
    2023-06-29
  • nodejs模板引擎有什么作用
    这篇文章主要讲解了“nodejs模板引擎有什么作用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“nodejs模板引擎有什么作用”吧! ...
    99+
    2022-10-19
  • 如何在PHP中使用模板引擎?
    随着网站的不断发展,许多开发人员开始使用模板引擎来更方便地管理和呈现网站内容。PHP作为一种非常流行的网站开发语言,也提供了许多模板引擎供开发者选择,例如Smarty、Twig和Blade等。在本篇文章中,我们将介绍如何在PHP中使用模板引...
    99+
    2023-05-14
    使用 PHP 模板引擎
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作