SpringBoot超详细讲解Thymeleaf模板引擎

Jsp是最早的模板技术,用来处理视图层的,用来做数据显示的模板

B S结构:

B:浏览器:用来显示数据,发送请求,没有处理能力

发送一个请求,访问a.jsp,a.jsp在服务器端变成Servlet,在将输出的数据返回给浏览器,浏览器就可以看到结果数据,jsp最终翻译过来也是个html页面

模板技术你就可以把它们当成字符串的替换,比如说:这里{data}这里有一个字符串,你把它换成固定值其他值,但是这个替换有一些附加的功能,通过模板技术处理视图层的内容

第一个例子:

pom.xml:Thymeleaf依赖:

<?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>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bjpowernode</groupId>
    <artifactId>027-thymeleaf-first</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--模板引擎起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

创建Controller控制器:HelloThymeleafController:

package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
    @RequestMapping("/hello")
    public String helloThymeleaf(HttpServletRequest request){
        //添加数据到request作用域,模板引擎可以从request中获取数据
        request.setAttribute("data","欢迎使用Thymeleaf模板引擎");
        //指定视图 模板引擎使用的页面(html)
        //逻辑名称
        return "hello";
    }
}

templates:用来放模板用到的视图文件的,模板引擎用到的模板到放在了template目录之下:

创建hello.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h3>使用Thymeleaf的例子</h3>
   <!--使用模板th:text=""获取数据-->
   <p th:text="${data}">想显示数据</p>
</body>
</html>

运行主启动类Application:

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

可以在hello.html中加入:未解决在标签中th爆红,和写的时候没有提示信息

xmlns:th="http://www.thymeleaf.org" 在标签中,再次写th的时候就有提示记录了

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h3>使用Thymeleaf的例子</h3>
   <!--使用模板th:text="${data}"获取后端request的作用域中的数据,把data数据替换文本,text表示取数据-->
   <p th:text="${data}">想显示数据</p>
   <p th:text="${data}">显示</p>
</body>
</html>

使用Model:

在Controller:

package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
    @RequestMapping("/hello")
    public String helloThymeleaf(Model model, HttpServletRequest request){
        //添加数据到request作用域,模板引擎可以从request中获取数据
        request.setAttribute("data","欢迎使用Thymeleaf模板引擎");
        //使用model和request作用域是一样的 实际上model中的数据就是放到request作用域中的
        model.addAttribute("mydata","model中的数据");
        //指定视图 模板引擎使用的页面(html)
        //逻辑名称
        return "hello";
    }
}

hello.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h3>使用Thymeleaf的例子</h3>
   <!--使用模板th:text="${data}"获取后端request的作用域中的数据,把data数据替换文本,text表示取数据-->
   <p th:text="${data}">想显示数据</p>
   <p th:text="${mydata}">显示</p>
</body>
</html>

speingboot配置文件application.properties:模板引擎的常用相关设置,基本不用设置都是默认的:

#在开发阶段,关闭模板发缓存,让修改立即生效 设置为true时,就是使用模板缓存,当第二次访问的时候,使用内存中的数据,就不在解析模板了
spring.thymeleaf.cache=false
#编码格式
spring.thymeleaf.encoding=UTF-8
#模板的类型(默认是 html,模板是html文件 它不仅支持网页做模板还支持其他类别的)
spring.thymeleaf.model=HTML
#模板的前缀:默认是类路径的classpath:/templates目录下
spring.thymeleaf.prefix=classpath:/templates/
#后缀
spring.thymeleaf.suffix=.html

到此这篇关于SpringBoot超详细讲解Thymeleaf模板引擎的文章就介绍到这了,更多相关SpringBoot Thymeleaf模板引擎内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • springboot学习之Thymeleaf模板引擎及原理介绍

    目录 模板引擎 什么是模板引擎? 模板引擎的原理 引入Thymeleaf Thymeleaf分析 Thymeleaf语法学习 模板引擎 springboot我们目前是以jar包的形式打包,实际上我们之前是打成war包,放到tomcat服务器里面,可以用JSP.但是jar包就导致不能用JSP,换一种方式就是springboot推荐的Thymeleaf模板引擎(JSP也是一种模板引擎,除此之外还有什么framework也是一种模板引擎), 什么是模板引擎? 模板引擎就是解决我们需要动态赋值给前端的一

  • 详解SpringBoot+Thymeleaf 基于HTML5的现代模板引擎

    序言: Thymeleaf 是Java服务端的模板引擎,与传统的JSP不同,前者可以使用浏览器直接打开,因为可以忽略掉拓展属性,相当于打开原生页面,给前端人员也带来一定的便利.如果你已经厌倦了JSP+JSTL的组合,Thymeleaf或许是个不错的选择!本工程传送门:SpringBoot-Web-Thymeleaf 开始使用 1.引入依赖 SpringBoot默认提供了Thymeleaf的Starter,只需简单引入依赖即可. <dependency> <groupId>org.s

  • SpringBoot使用Thymeleaf模板引擎访问静态html的过程

    最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的傻瓜教程..... 一:创建spring boot的web项目,过程略: 二:依赖如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <

  • Java SpringBoot模板引擎之 Thymeleaf入门详解

    目录 模板引擎简介 引入Thymeleaf模板引擎 分析Thymeleaf模板引擎 测试Thymeleaf模板引擎 1.编写一个TestController 2.编写一个测试页面 test.html 放在 templates 目录下 3.启动项目请求测试 4.结论 Thymeleaf入门: 1.修改测试请求,增加数据传输 2.使用thymeleaf 3.我们去编写下前端页面 4.启动测试! thymeleaf语法学习 1.使用任意的 th:attr 来替换Html中原生属性的值! 2.表达式语法

  • SpringBoot超详细讲解Thymeleaf模板引擎

    Jsp是最早的模板技术,用来处理视图层的,用来做数据显示的模板 B S结构: B:浏览器:用来显示数据,发送请求,没有处理能力 发送一个请求,访问a.jsp,a.jsp在服务器端变成Servlet,在将输出的数据返回给浏览器,浏览器就可以看到结果数据,jsp最终翻译过来也是个html页面 模板技术你就可以把它们当成字符串的替换,比如说:这里{data}这里有一个字符串,你把它换成固定值其他值,但是这个替换有一些附加的功能,通过模板技术处理视图层的内容 第一个例子: pom.xml:Thymele

  • SpringBoot超详细讲解集成Flink的部署与打包方法

    目录 一.SpringBoot集成Flink 二.FlinkTask写法调整 三.打包插件 四.Flink的上传与运行 总结 一.SpringBoot集成Flink 其实没什么特别的,就把Flink依赖的包在pom引入就行了.只是FlinkTask的写法要小调整下,把相关依赖交给spring管理就行. 然后如果放弃Flink的Dashboard端监控task执行相关信息,那也可以在SpringBoot的启动类里调用就行,但是可能出现task的相关对象没有注入,这种都是小问题(实际就是spring

  • SpringBoot超详细讲解多数据源集成

    目录 一.多数据源使用场景与弊端 1.场景 2.弊端 二.使用步骤 1.引入库 2.多数据源配置文件 3.多数据源配置类 4.使用 总结 一.多数据源使用场景与弊端 1.场景 业务系统跨数据库 数据转存(这个现在太low了,应该高级点都不用) 系统集成 2.弊端 跨库业务事务问题 service.dao不能重复注入数据源 二.使用步骤 1.引入库 <!-- 多数据源支持 --> <dependency> <groupId>com.baomidou</groupId

  • SpringBoot超详细讲解yaml配置文件

    目录 1.文件类型 A.properties配置文件类型 B.yaml 基本语法 数据类型 2.配置提示 1.文件类型 A.properties配置文件类型 同以前properties用法一样 B.yaml 简介: YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写.在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言). 非常适合用来做以

  • SpringBoot超详细讲解自动配置原理

    目录 SpringBoot自动配置原理 SpringBoot特点 1.依赖管理 A.父项目做依赖管理 B.开发导入starter场景启动器 C.可以修改默认版本号 2.自动配置 A.自动配好Tomcat B.自动配好SpringMVC C.默认的包结构 D.各种配置拥有默认值 E.按需要加载所有自动配置项 SpringBoot自动配置原理 了解SpringBoot自动配置原理 1.SpringBoot特点 2.容器功能 3.自动配置原理入门 4.开发技巧 SpringBoot特点 1.依赖管理

  • SpringBoot超详细讲解@Enable*注解和@Import

    目录 @Enable* 解决办法 解放方案一 解决方案二 解决方案三 @Import 1.导入Bean 2.导入配置类 3.导入ImportSelector实现类 4.导入ImportBeanDefinitionRegistrar实现类 @Enable* 创建一个主启动类 package com.you.boot; import com.you.config.EnableUser; import com.you.config.UserConfig; import org.springframew

  • SpringBoot超详细讲解@Value注解

    目录 一.非配置文件注入 1.注入普通字符串 2.注入JAVA系统变量 3.注入表达式 4.注入其他Bean属性 5.注入文件资源 6.注入URL资源 二.通过配置文件注入 1.注入普通字符串 2.注入基本类型 3.注入数组类型 4.注入List类型 5.注入Map类型 一.非配置文件注入 1.注入普通字符串 直接附在属性名上,在 Bean 初始化时,会赋初始值. @Value("admin") private String name; 2.注入JAVA系统变量 @Value(&quo

  • SpringBoot超详细讲解事务管理

    目录 1. 事务的定义 2. 事务的特性 3. 事务的隔离性 4. 事务管理 5. 示例 1. 事务的定义 事务是由 N 步数据库操作序列组成的逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行. 2. 事务的特性 事务的 ACID 特性: 原子性:事务是应用中不可分割的最小执行体 一致性:事务执行的结果必须使得数据从一个一致性状态转变为另一个一致性状态 隔离性:各个事务的执行互不干扰,任何事务的内部操作对其他事务都是隔离的 持久性:事务一旦提交,对数据所做的任何修改都要记录到永久存储器中

  • Spring Boot thymeleaf模板引擎的使用详解

    在早期开发的时候,我们完成的都是静态页面也就是html页面,随着时间轴的发展,慢慢的引入了jsp页面,当在后端服务查询到数据之后可以转发到jsp页面,可以轻松的使用jsp页面来实现数据的显示及交互,jsp有非常强大的功能,但是,在使用springboot的时候,整个项目是以jar包的方式运行而不是war包,而且还嵌入了tomcat容器,因此,在默认情况下是不支持jsp页面的.如果直接以纯静态页面的方式会给我们的开发带来很大的麻烦,springboot推荐使用模板引擎. 模板引擎有很多种,jsp,

  • SpringBoot详细讲解视图整合引擎thymeleaf

    目录 1. 支持的视图技术 2. Thymeleaf 2.1 Thymeleaf语法 2.2 标准表达式 1. 变量表达式 ${…} 2. 选择变量表达式 *{…} 3. 消息表达式 #{…} 4. 链接表达式 @{…} 5. 片段表达式 ~{…} 3. 基本使用 3.1 Thymeleaf模板基本配置 3.2 静态资源的访问 3.3 完成数据的页面展示 1. 创建Spring Boot项目 2. 编写配置文件 3. 创建web控制类 4. 创建模板页面并引入静态资源文件 5.效果测试 1. 支

随机推荐