springboot开发之thymeleaf模板引擎

西西嘛呦      2022-05-11     436

关键词:

1、引入thymeleaf

在pom.xml中写入:

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

2、thymeleaf语法

HelloController.java

package com.gong.springbootcurd.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class HelloController {

    @RequestMapping("/login")
    public String success(Map<String,Object> map){
        map.put("hello","你好");
        return "success";
    }

}

thymeleaf会默认访问classpath:/templates/下的html文件,因此发送/login请求时会返回/templates/success.html

success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h4>success page</h4>
    <div th:text="${hello}">
    </div>
</body>
</html>

(1)th:任意html属性;来替换原来属性的值

比如说th:id="${hello}" th:class="${hello}",可以替换id和class里面的值,即此时id="你好",class="你好"

(2)th中的属性是有优先级的

th后面可以接:片段包含、遍历、条件判断、声明变量、属性修改、修改指定属性默认值、修改标签体内容、声明片段等等的属性。

(3)表达式语法

${...}:用于获取变量值(不仅可以获取对象的属性,还可以调用方法、使用内置的基本对象、使用工具对象)

*{...}:选择表达式(和${...}基本功能一致),可以配合th:object使用,简化写法

#{...}:用于获取国际化内容的

@{...}:定义url链接的

~{...}:片段引用表达式

表达式里面可以使用:字面量、文本操作、数学运算、布尔运算、比较运算、条件运算、三元运算符

简略看看其中的一些:

HelloController.java

package com.gong.springbootcurd.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.Map;

@Controller
public class HelloController {

    @RequestMapping("/login")
    public String success(Map<String,Object> map){
        map.put("hello","<h1>你好<h1>");
        map.put("users", Arrays.asList("张三","李四","王五"));
        return "success";
    }

}

success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h4>success page</h4>
    <div th:text="${hello}"></div>
    <div th:utext="${hello}"></div>
    <h4 th:text="${user}" th:each="user:${users}"></h4>
    <hr/>
    <h4>
        <span th:each="user:${users}">[[${user}]]</span>
    </h4>
</body>
</html>

说明:

th:text:会转义特殊字符

th:utext:不会转义特殊字符

th:each写在h4标签中,每次遍历都会生成一个h4标签。

th:each写在h4标签下的span标签中,每次遍历生成一个span标签。

在文中中获取变量的值要加上两个方括号:[[]]

运行之后查看效果:

 

springboot之thymeleaf模板

阅读目录一、前言二、集成Thymeleaf模板引擎三、使用Thymeleaf模板回到顶部一、前言Thymeleaf的出现是为了取代JSP,虽然JSP存在了很长时间,并在JavaWeb开发中无处不在,但是它也存在一些缺陷:1、JSP最明显的问题在于它看起来像HTML... 查看详情

springboot中添加thymeleaf模板

SpringBoot中添加Thymeleaf模板1.Web开发方式2.用SpringBoot创建带有Thymeleaf模板的web项目2.1用SpringInitializr方式创建springboot项目2.2创建出来的项目结构2.3创建html模板页面2.4创建控制层页面2.5运行1.Web开发方式Springboot提供了一套完整的web开... 查看详情

springboot中添加thymeleaf模板

SpringBoot中添加Thymeleaf模板前面我们讲解了SpringBoot项目的创建、SpringBoot结构信息,自动配置功能等,那么Springboot创建出来,我们最终是要做web开发的,所以我们这章讲解如何用SpringBoot做web开发。一.Web开发方式Springboot提供了一... 查看详情

springboot用thymeleaf模板的paginate分页

本文根据一个简单的user表为例,展示springboot集成mybatis,再到前端分页完整代码(新手自学,不足之处欢迎纠正);先看java部分pom.xml加入<!--支持Web应用开发,包含Tomcat和spring-mvc。--><dependency><groupId>org.springframework.... 查看详情

springboot2之web开发(下)——数据响应模板引擎拦截器文件上传和异常处理(未完成)(代码片段)

SpringBoot2之web开发(下)——数据响应、模板引擎、拦截器、文件上传和异常处理一、数据响应和内容协商1.1响应json数据1.2内容协商(待写)二、模板引擎(Thymeleaf)2.1模板引擎介绍2.2Thymeleaf语法2.2.1常用t... 查看详情

重学springboot系列之整合静态资源与模板引擎(代码片段)

重学SpringBoot系列之整合静态资源与模板引擎webjars与静态资源springboot静态资源favicon.ico图标欢迎页面使用WebJars管理css&js1.pom中引入依赖2.访问引入的js文件自动检测依赖的版本测试模板引擎选型与未来趋势javaweb开发经历的几个... 查看详情

springboot+gradle+thymeleaf搭配会如何——快速入门java模板开发

...。但是,很多新程序员还是存在一些学习障碍。本文结合SpringBoot和Gradle来逐步介绍Thymeleaf关键知识点,只要一天就能真正入门。​1.按部就班开发大表查询应用程序下面给出一个实例来巩固相关知识。我们的实例使用STS4.17(IDEA... 查看详情

springboot整合thymleaf模板引擎

thymeleaf作为springboot官方推荐使用的模板引擎,简单易上手,功能强大,thymeleaf的功能和jsp有许多相似之处,两者都属于服务器端渲染技术,但thymeleaf比jsp的功能更强大。1.thymeleaf入门1.1引入坐标<!--springBoot整合thymeleaf--><dep... 查看详情

springboot整合thymleaf模板引擎

thymeleaf作为springboot官方推荐使用的模板引擎,简单易上手,功能强大,thymeleaf的功能和jsp有许多相似之处,两者都属于服务器端渲染技术,但thymeleaf比jsp的功能更强大。1.thymeleaf入门1.1引入坐标<!--springBoot整合thymeleaf--><dep... 查看详情

springboot系列——模板引擎thymeleaf

〇、thymeleaf是什么  1.在以往开发springweb项目时,若我们想在前端页面上显示一些服务端的数据(即动态显示),得借助JSP的内置对象和JSTL实现,或者通过JavaScript请求实现;其缺点在于,与后端联系太紧密,不利于前后端分... 查看详情

springboot2系列教程|整合thymeleaf

...eaf,并整合Thymeleaf开发一个简陋版的学生信息管理系统。SpringBoot提供了大量模板引擎,包含Freemarker、Groovy、Thymeleaf、Velocity以及Mustache,SpringBoot中推荐使用Thymeleaf作为模板引擎,因为Thymeleaf提供了完美的SpringMVC支持。Thymeleaf是... 查看详情

springboot整合thymeleaf

...中前后端分离是趋势,但是视图层技术还占有一席之地,SpringBoot对视图层技术提供了很好的支持,官方推荐使用的模板引擎是Thymeleaf不过像FreeMarker也支持,JSP技术在这里并不推荐使用。  Thymeleaf是新一代Java模板引擎,类似于... 查看详情

「springboot实战」视图技术-thymeleaf

...模板技术,从而实现前后端分离和页面的动态数据展示。SpringBoot框架为简化项目的整体开发,提供了一些视图技术支持,并主要推荐整合模板引擎技术实现前端页面的动态化内容。本文对SpringBoot常用的Thymeleaf进行整合。Thymeleaf... 查看详情

springall(1-12)复习(代码片段)

1-12复习详细解释Thymeleaf月springboot的关系Thymeleaf是一个现代化的Java模板引擎,它允许开发人员使用自然模板语言来创建动态网页。而SpringBoot是一个快速开发框架,它简化了Spring应用程序的配置和部署,使得开发人员可以更加专... 查看详情

springboot页面展示thymeleaf

...统JavaWEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了。SpringBoot支持如下页面模板语言上面并没有列举所有SpringBoot支持的页面模板技术。其中Thymeleaf是SpringBoot官方所推荐使用的,下面来谈谈Thymeleaf... 查看详情

springboot?使用thymeleaf模板引擎渲染web视图

...引用大量的js、css、图片等静态资源。 默认配置 SpringBoot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static/public/resources/META-INF/resources 举例:我们可以在src/main/resources/目 查看详情

bkt项目搭建环境并测试之3添加日志和thymeleaf模板(代码片段)

简介:这个项目是为了学习SpringBoot以及学习SpringCloud用的,如果对你有什么帮助,还是非常高兴的。GitHub:  https://github.com/fankf/bkt.git技术使用:SpringBoot+SSM+MySql+ThymeleafIDE: STS日志相关内容:添加logging相关:<!--日志默... 查看详情

springboot第九章thymeleaf模板引擎(代码片段)

...ymeleafThymeleaf是另外的一种模板技术,它本身并不属于SpringBoot,SpringBoot只是很好地集成这种模板技术,作为前端页面的数据展示。使用java开发的模板技术,在服务器端运行。把处理后的数据发送给浏览器。模板是... 查看详情