SpringBoot中使用Thymeleaf模板详情

目录
  • 一.什么是Thymeleaf
  • 二.SpringBoot中使用Thymeleaf模板
    • 1.pom.xml中添加thymeleaf依赖
    • 2.关闭thymeleaf缓存
    • 3.创建thymeleaf模板页面
    • 4.创建一个类(用于与上述html页面交互)
    • 5.访问服务路径

一.什么是Thymeleaf

官网原话:Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。 Thymeleaf的主要目标是提供一种优雅且高度可维护的模板创建方式。为此,它以自然模板的概念为基础,以不影响模板用作设计原型的方式将其逻辑注入模板文件。这样可以改善设计沟通,并缩小设计团队与开发团队之间的差距。Thymeleaf是一个HTML5模板引擎,可用于Web环境中的应用开发。Thymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式。thymeleaf模板引擎,替代jsp。

二.SpringBoot中使用Thymeleaf模板

1.pom.xml中添加thymeleaf依赖

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

2.关闭thymeleaf缓存

在application.yml中的spring:下添加如下代码(能让改动的页面及时生效,实现类似热部署效果):

#能让改动的页面及时生效,实现类似热部署效果
thymeleaf:
    cache: false

注意缩进,添加后缩进如下:

3.创建thymeleaf模板页面

创建一个普通的html文件hello.html,如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
</html>

在html的标签上加入名称空间xmlns:th="http://www.thymeleaf.org"表示该页面是一个thymeleaf模板页面。 即把上述代码中<html lang="en">换成<html lang="en" xmlns:th="http://www.thymeleaf.org"> 这样就可以在页面中的标签内使用th属性取出model中的值,类似于EL表达式。 具体用法代码如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p th:text="'欢迎来到中国,我叫'+${name}+',今年'+${age}+'岁。'"></p>
    <p>欢迎来到中国,我叫<span th:text="${name}"></span>,今年<span th:text="${age}"></span>岁。</p>
</body>
</html>

4.创建一个类(用于与上述html页面交互)

ackage com.ysw.springboot01.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/thy")
public class ThymeleafController {
    @RequestMapping("/hello")
    public String hello0(Model model){
        //向model中存入数据
        model.addAttribute("name","李白");
        model.addAttribute("age","18");
        //跳转到hello.html模版引擎
        return "hello";
    }
}

5.访问服务路径

效果如下:

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

(0)

相关推荐

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

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

  • springboot中thymeleaf模板使用详解

    这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎. thymeleaf介绍 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页

  • Springboot使用thymeleaf动态模板实现刷新

    当springmvc转向springboot的时候,发现springboot默认支持thymeleaf,对于一直使用jsp的程序员,还是有点不适应. 不过研究了一会发现thymeleaf更加适合前后分离,所以果断抛弃jsp转入thymeleaf,最头疼的就是修改完之后不会自动刷新. 首先要在配置文件application.yml (或 application.properties) spring thymeleaf: cache: false #重点是这个要设置false不然会无效 prefix

  • springboot如何使用thymeleaf模板访问html页面

    引言 在传统的web开发中通常使用jsp页面,首先需要在pom文件中引入springmvc相关的包,然后写springmvc的配置文件(包括访问资源的路径解析),之后还需再web.xml中配置访问路由.这无疑太麻烦了,每次开发前都需要编写大量的配置文件. springboot为此提供了高效便捷的解决方案,只需再pom.xml中添加web开发的依赖,便可进行web开发,省去了繁琐的配置步骤. 下面为web开发引入的依赖 <dependency> <groupId>org.spring

  • SpringBoot使用thymeleaf模板过程解析

    这篇文章主要介绍了SpringBoot使用thymeleaf模板过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导入依赖 <!-- 添加thymeleaf模版的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</art

  • SpringBoot中使用Thymeleaf模板详情

    目录 一.什么是Thymeleaf 二.SpringBoot中使用Thymeleaf模板 1.pom.xml中添加thymeleaf依赖 2.关闭thymeleaf缓存 3.创建thymeleaf模板页面 4.创建一个类(用于与上述html页面交互) 5.访问服务路径 一.什么是Thymeleaf 官网原话:Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本. Thymeleaf的主要目标是提供一种优雅且高度

  • SpringBoot中的Thymeleaf模板

    一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或XML,但它其实上并不是.大多数的JSP模板都是采用HTML的形式,但是又掺杂上了各种JSP标签库的标签,使其变得很混乱. 2.JSP 规范是与 Servlet 规范紧密耦合的.这意味着它只能用在基于 Servlet 的Web应用之中.JSP模板不能作为通用的模板(如格式化Email),也不能用于非S

  • SpringBoot中的Thymeleaf用法

    Thymeleaf Thymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里. 我们为什么要用Thymeleaf来作为模板引擎呢?官网给了我们一个非常令人信服的解释: Thymeleaf is a modern server-side Java template engine for both web and standalone environments.> 基本写法就像下面这样: <table> <thead> <tr&g

  • SpringMVC中使用Thymeleaf模板引擎实例代码

    本文研究的主要是SpringMVC中使用Thymeleaf模板引擎的相关内容,具体介绍如下. Thymeleaf提供了一组Spring集成,允许您将其用作Spring MVC应用程序中全面替代JSP的功能. Maven依赖 <!-- thymeleaf-spring4 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifa

  • 浅析SpringBoot中使用thymeleaf找不到.HTML文件的原因

    thymeleaf是目前最新的模板引擎,它和springboot完美搭配,让前后端不在疏远 首先先介绍一下市面上出现过的模板引擎 1, Verlocity 距今有10多年之久,是由Apache推出的 Struts2 底层使用2,Freemarker,距今有10多年之久, 第三方小公司推出,后来得到了Apache的认可,在Struts2这个框架中, 底层使用Freemarker. SpringBoot 默认使用Freemarker是以.ftl结尾的文件. 3,Thymeleaf是以.html结尾的

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

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

  • spring boot 项目中使用thymeleaf模板的案例分析

    准备 MySql数据库,表Prereg,IDEA 数据库中的表如下所示: IDEA目录结构如下: 添加thymeleaf依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 开始添加代码: 在controller包添加类"

  • 在SpringBoot中配置Thymeleaf的模板路径方式

    目录 配置Thymeleaf的模板路径 关于thymeleaf配置说明 配置Thymeleaf的模板路径 众所周知,Thymeleaf的模板文件默认是在项目文件夹的src\main\resources\templates目录下的. 不过出于特殊需要,要修改其路径怎么办呢? 在我们的项目配置文件application.properties中,添加如下配置: #Thymeleaf配置 spring.thymeleaf.prefix=自定义的Thymeleaf的模板位置,jar内部以classpath

  • springBoot加入thymeleaf模板的方式

    1.新建springBoot项目 在前面有两种方式 2.加入thymeleaf模板引擎 SpringBoot推荐使用thymeleaf模板引擎 语法简单,功能更强大 要想引入thymeleaf,只需要在pom,xml文件中加入如下依赖就可以了 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifa

随机推荐