`

thymeleaf

 
阅读更多


maven

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


可以查看依赖关系,发现spring-boot-starter-thymeleaf下面已经包括了spring-boot-starter-web,所以可以把spring-boot-starter-web的依赖去掉.

2.配置视图解析器

spring-boot很多配置都有默认配置,比如默认页面映射路径为
classpath:/templates/*.html
同样静态文件路径为
classpath:/static/

在application.properties中可以配置thymeleaf模板解析器属性.就像使用springMVC的JSP解析器配置一样

#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end

具体可以配置的参数可以查看
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类,上面的配置实际上就是注入到该类中的属性值.

3.编写DEMO

1.控制器

    @Controller
    public class HelloController {

        private Logger logger = LoggerFactory.getLogger(HelloController.class);
        /**
         * 测试hello
         * @return
         */
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public String hello(Model model) {
            model.addAttribute("name", "Dear");
            return "hello";
        }

    }

2.view(注释为IDEA生成的索引,便于IDEA补全)

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<!--/*@thymesVar id="name" type="java.lang.String"*/-->
<p th:text="'Hello!, ' + ${name} + '!'" >3333</p>
</body>
</html>

3.效果

这里写图片描述

4.基础语法

回味上面的DEMO,可以看出来首先要在改写html标签

<html xmlns:th="http://www.thymeleaf.org">
1
2
这样的话才可以在其他标签里面使用th:*这样的语法.这是下面语法的前提.

1.获取变量值

<p th:text="'Hello!, ' + ${name} + '!'" >3333</p>
1
2
可以看出获取变量值用$符号,对于javaBean的话使用变量名.属性名方式获取,这点和EL表达式一样.

另外$表达式只能写在th标签内部,不然不会生效,上面例子就是使用th:text标签的值替换p标签里面的值,至于p里面的原有的值只是为了给前端开发时做展示用的.这样的话很好的做到了前后端分离.

2.引入URL

Thymeleaf对于URL的处理是通过语法@{…}来处理的

<a th:href="@{http://blog.csdn.net/u012706811}">绝对路径</a>
<a th:href="@{/}">相对路径</a>
<a th:href="@{css/bootstrap.min.css}">Content路径,默认访问static下的css文件夹</a>
1
2
3
类似的标签有:th:href和th:src

3.字符串替换

很多时候可能我们只需要对一大段文字中的某一处地方进行替换,可以通过字符串拼接操作完成:

<span th:text="'Welcome to our application, ' + ${user.name} + '!'">
1
2
一种更简洁的方式是:

<span th:text="|Welcome to our application, ${user.name}!|">
1
2
当然这种形式限制比较多,|…|中只能包含变量表达式${…},不能包含其他常量、条件表达式等。

4.运算符

在表达式中可以使用各类算术运算符,例如+, -, *, /, %

th:with="isEven=(${prodStat.count} % 2 == 0)"
1
2
逻辑运算符>, <, <=,>=,==,!=都可以使用,唯一需要注意的是使用<,>时需要用它的HTML转义符:

    th:if="${prodStat.count} &gt; 1"
    th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"
1
2
5.条件

if/unless

Thymeleaf中使用th:if和th:unless属性进行条件判断,下面的例子中,标签只有在th:if中条件成立时才显示:

<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
1
2
th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。

Switch

Thymeleaf同样支持多路选择Switch结构:

<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
</div>

默认属性default可以用*表示:

<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p>
</div>

6.循环

渲染列表数据是一种非常常见的场景,例如现在有n条记录需要渲染成一个表格

,该数据集合必须是可以遍历的,使用th:each标签:
<body>
  <h1>Product list</h1>

  <table>
    <tr>
      <th>NAME</th>
      <th>PRICE</th>
      <th>IN STOCK</th>
    </tr>
    <tr th:each="prod : ${prods}">
      <td th:text="${prod.name}">Onions</td>
      <td th:text="${prod.price}">2.41</td>
      <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
    </tr>
  </table>

  <p>
    <a href="../home.html" th:href="@{/}">Return to home</a>
  </p>
</body>

可以看到,需要在被循环渲染的元素(这里是)中加入th:each标签,其中th:each=”prod : ${prods}”意味着对集合变量prods进行遍历,循环变量是prod在循环体中可以通过表达式访问。

7.Utilities

为了模板更加易用,Thymeleaf还提供了一系列Utility对象(内置于Context中),可以通过#直接访问:

#dates
#calendars
#numbers
#strings
arrays
lists
sets
maps

下面用一段代码来举例一些常用的方法:
date

/*
* Format date with the specified pattern
* Also works with arrays, lists or sets
*/
${#dates.format(date, 'dd/MMM/yyyy HH:mm')}
${#dates.arrayFormat(datesArray, 'dd/MMM/yyyy HH:mm')}
${#dates.listFormat(datesList, 'dd/MMM/yyyy HH:mm')}
${#dates.setFormat(datesSet, 'dd/MMM/yyyy HH:mm')}

/*
* Create a date (java.util.Date) object for the current date and time
*/
${#dates.createNow()}

/*
* Create a date (java.util.Date) object for the current date (time set to 00:00)
*/
${#dates.createToday()}

string

/*
* Check whether a String is empty (or null). Performs a trim() operation before check
* Also works with arrays, lists or sets
*/
${#strings.isEmpty(name)}
${#strings.arrayIsEmpty(nameArr)}
${#strings.listIsEmpty(nameList)}
${#strings.setIsEmpty(nameSet)}

/*
* Check whether a String starts or ends with a fragment
* Also works with arrays, lists or sets
*/
${#strings.startsWith(name,'Don')}                  // also array*, list* and set*
${#strings.endsWith(name,endingFragment)}           // also array*, list* and set*

/*
* Compute length
* Also works with arrays, lists or sets
*/
${#strings.length(str)}

/*
* Null-safe comparison and concatenation
*/
${#strings.equals(str)}
${#strings.equalsIgnoreCase(str)}
${#strings.concat(str)}
${#strings.concatReplaceNulls(str)}

/*
* Random
*/
${#strings.randomAlphanumeric(count)}
分享到:
评论

相关推荐

    Thymeleaf中文参考手册_3.0.5版_thymeleaf_thymeleaf中文网_thymeleaf文档_thmele

    Thymeleaf的设计理念是使得静态模板在脱离服务器环境时也能正常工作,提供了一种易于阅读和维护的静态代码形式。其3.0.5版是该技术的一个稳定版本,具有诸多增强和改进。 Thymeleaf的主要特性包括: 1. **模板语言...

    thymeleaf-jar包

    在给定的"thymeleaf-jar包"中,包含了一系列依赖的jar文件,这些都是Thymeleaf正常工作所必需的库: 1. `thymeleaf-3.0.12.RELEASE.jar`:这是Thymeleaf的核心库,包含了所有处理模板和表达式的类。版本号3.0.12...

    Thymeleaf中文文档合集-最新版

    Thymeleaf是一款强大的Java模板引擎,常用于Web应用程序,特别是Spring MVC框架中,替代了JSP作为视图层技术。这个"Thymeleaf中文文档合集-最新版"包含了一系列的学习资源,如PDF、PPT等,帮助开发者深入理解和掌握...

    thymeleaf-3.0.3.RELEASE-API文档-中文版.zip

    赠送jar包:thymeleaf-3.0.3.RELEASE.jar; 赠送原API文档:thymeleaf-3.0.3.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-3.0.3.RELEASE-sources.jar; 包含翻译后的API文档:thymeleaf-3.0.3.RELEASE-javadoc-...

    thymeleaf-3.0.12.RELEASE-API文档-中文版.zip

    赠送jar包:thymeleaf-3.0.12.RELEASE.jar; 赠送原API文档:thymeleaf-3.0.12.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-3.0.12.RELEASE-sources.jar; 赠送Maven依赖信息文件:thymeleaf-3.0.12.RELEASE.pom;...

    thymeleaf-3.0.11.RELEASE_release_spring_thymeleaf_

    在Spring框架中,Thymeleaf通常作为视图解析器使用,为开发人员提供了便捷的方式来构建动态Web页面。Thymeleaf-3.0.11.RELEASE是该引擎的一个特定版本,它包含了优化和修复的特性,以提高性能和稳定性。 1. **...

    thymeleaf-3.0.9.RELEASE-API文档-中文版.zip

    赠送jar包:thymeleaf-3.0.9.RELEASE.jar; 赠送原API文档:thymeleaf-3.0.9.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-3.0.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:thymeleaf-3.0.9.RELEASE.pom; ...

    spring mvc整合thymeleaf示例

    thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要高,而且把将美工和程序员能够结合起来,美工能够在浏览器中查看静态效果,程序员可以在应用服务器查看带数据的效果。 thymeleaf是一个...

    thymeleaf使用+API

    Thymeleaf是一个面向web和独立环境的现代服务器端Java模板引擎。 Thymeleaf的主要目标是将优雅的自然模板引入到您的开发工作流程中——可以在浏览器中正确显示的HTML,也可以作为静态原型工作,从而在开发团队中实现...

    springboot整合thymeleaf完整例子

    本案例将详细讲解如何将SpringBoot与Thymeleaf进行整合,构建一个完整的Web应用。 首先,我们需要在SpringBoot项目中引入Thymeleaf的依赖。在`pom.xml`文件中,添加如下Maven依赖: ```xml &lt;groupId&gt;org.spring...

    springboot整合thymeleaf模板

    SpringBoot整合Thymeleaf模板是一项常见的Web开发任务,它结合了SpringBoot的便捷性和Thymeleaf的动态模板引擎,使得开发人员可以快速构建功能丰富的Web应用。下面将详细介绍这个过程及其涉及的关键知识点。 首先,...

    springboot与thymeleaf结合的实战、实例项目

    SpringBoot和Thymeleaf是两个非常流行的Java技术,它们在现代Web开发中扮演着重要角色。SpringBoot简化了Spring框架的配置,提供了快速启动和运行应用的方式,而Thymeleaf则是一种强大的模板引擎,它允许开发者在...

    thymeleaf_3.0.5_中文参考手册.pdf

    Thymeleaf 3.0.5 中文参考手册 Thymeleaf 是一个基于模板引擎的Java模板引擎,可以处理各种模板,例如HTML、XML、TXT等。Thymeleaf 的主要特点是它可以将模板解析为静态 HTML,然后将其传递给浏览器,从而提高页面...

    Spring boot + thymeleaf 后端直接给onclick函数赋值的实现代码

    "Spring Boot + Thymeleaf 实现后端直接给 onclick 函数赋值的知识点" Spring Boot 是一个基于 Java 的框架,用于构建基于 Web 的应用程序,而 Thymeleaf 是一个基于 XML 的模板引擎,用于生成 HTML 内容。在 ...

    thymeleaf-spring5-3.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:thymeleaf-spring5-3.0.10.RELEASE.jar; 赠送原API文档:thymeleaf-spring5-3.0.10.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-spring5-3.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:...

    Thymeleaf所需要的jar包

    - `thymeleaf-spring5.jar`(或`thymeleaf-servlet.jar`,取决于Spring版本):这个库是Thymeleaf与Spring框架集成的关键。它允许Thymeleaf与Spring MVC无缝协作,提供模型数据注入、视图解析等功能。 4. **...

    Thymeleaf开发手册中文版

    ### Thymeleaf开发手册中文版知识点概览 #### 1. Thymeleaf简介 ##### 1.1 Thymeleaf是什么 Thymeleaf 是一个现代的、开源的 Java 模板引擎,适用于 Web 和非 Web 环境(如电子邮件、纯文本消息、PDF 文档等)。...

    thymeleaf-spring5-3.0.12.RELEASE-API文档-中文版.zip

    赠送jar包:thymeleaf-spring5-3.0.12.RELEASE.jar; 赠送原API文档:thymeleaf-spring5-3.0.12.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-spring5-3.0.12.RELEASE-sources.jar; 赠送Maven依赖信息文件:...

    thymeleaf框架通过java渲染html生成pdf

    首先,让我们理解Thymeleaf的工作原理。Thymeleaf允许开发者使用HTML作为模板语言,通过在HTML中插入特定的Thymeleaf表达式,将静态HTML转变为动态内容。这些表达式可以与后端的Java对象交互,填充数据到模板中,...

    thymeleaf-extras-java8time-3.0.1.RELEASE-API文档-中文版.zip

    赠送jar包:thymeleaf-extras-java8time-3.0.1.RELEASE.jar; 赠送原API文档:thymeleaf-extras-java8time-3.0.1.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-extras-java8time-3.0.1.RELEASE-sources.jar; 赠送...

Global site tag (gtag.js) - Google Analytics