springboot 整合Thymeleaf

Thymeleaf是目前流行的视图层的技术,Spring boot 官方推荐的使用Thymeleaf

什么是Thymeleaf?

Thymeleaf是一个支持原生的THML文件的java末班,可以实现前后端的分离的交互方式,即视图与业务的数据分开响应,他可以直接返回服务端返回的数据生成HTML文件,同时也可以处理XML、javascript、css等格式。

Thymeleaf的最大特点是即可以直接在浏览器打开(静态方式),也可以结合服务器将业务数据填充到HTML之后启动动态的页面(动态方式),Springboot支持Thymeleaf,使用起来非常方便。

1.创建maven工程

<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

2.application.yml

spring:
thymeleaf:
prefix: classpath:/templates/ #模版的路径
suffix: .html #模版的后缀
servlet:
content-type: text/html #设置Content-type
encoding: UTF-8 #编码方式
mode: HTML5 #校验H5的格式
cache: false #关闭缓冲 每次都重新修改页面

3.创建Handler

package com.southwind.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/hello")
public class HellloHandler {
@GetMapping("/index")
public ModelAndView index(){
ModelAndView modelAndView =new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("mess","张三");
return modelAndView;
}
}

4.启动类

package com.southwind;

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);
}
}

5.视图:

HTML

<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymaleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<p th:text="${mess}">hello word</p>
</body>
</html>

需要引入标签

<html xmlns:th="http://www.thymaleaf.org">
  <p th:text="${mess}">hello word</p>

Thymeleaf是直接嵌入到模版标签的内部的,不同于JSTL模版

Thymeleaf常用标签

  • th:text

    th:text用于文本的显示,将业务的值显示到HTML的页面中

  • th:if

    th:if用于条件判断,对业务数据的判断,如果条件成立,则显示内容,否则不显示,具体的使用:

    @GetMapping("/if")
    public ModelAndView ifTest(){
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.setViewName("test");
    modelAndView.addObject("score",90);
    return modelAndView;
    }

    test.html

    <!DOCTYPE html>
    <html lang="en">
    <html xmlns:th="http://www.thymaleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <p th:if="${score>=90}">优秀</p>
    <p th:if="${score<90}">良好</p>
    </body>
    </html>
  • th:unless

    th:unless也用作条件判断,逻辑于if恰好相反,如果条件成立不显示,条件不成立显示

    @GetMapping("/unless")
    public ModelAndView unlessTest(){
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.setViewName("test");
    modelAndView.addObject("score",90);
    return modelAndView;
    }
    <!--unless-->
    <p th:unless="${score>=90}">优秀</p>
    <p th:unless="${score<90}">良好</p>
  • th:switch th:case

th:switch th:case两结合起来使用,用于多条件的等值判断,逻辑于java的switch case一致,当switch中的业务数据等于摸个case时,就显示该case对应的内容。

@GetMapping("/switch")
public ModelAndView switchTest(){
ModelAndView modelAndView =new ModelAndView();
modelAndView.setViewName("test");
modelAndView.addObject("mess",1);
return modelAndView;
}
<!--switch-->
<div th:switch="${mess}">
<p th:case="1">优秀</p>
<p th:case="2">良好</p>
</div>
  • th:action

    用来指定请求的URL,相当于form表单的action属性

    1.写死

    <form th:action="@{/hello/action}" method="get">
    <input type="submit" value="提交">
    </form>

2.后端传过来

<form th:action="${url}" method="get">
<input type="submit" value="提交">
</form>
@GetMapping("/redirect/{url}")
public String redirect(@PathVariable("url") String url, Model model){
model.addAttribute("url" ,"/hello/action");
return url;
}
@GetMapping("/action")
@ResponseBody
public String actionTest(){
return "action";
}
  • th;each

    用来遍历集合

    1.实体类

    package com.southwind.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data; @Data
    @AllArgsConstructor
    public class User {
    private String name;
    private Integer id;
    }

    2.Handler

    @GetMapping("/each")
    public ModelAndView each(){
    List<User> users = Arrays.asList(new User("张三",1),new User("李四",2),new User("王五",3));
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.setViewName("test");
    modelAndView.addObject("list",users);
    return modelAndView;
    }

    3.视图

    <!--each-->
    <table>
    <tr>
    <th> 编号</th>
    <th> 姓名</th>
    </tr>
    <tr th:each="user:${list}">
    <td th:text="${user.id}"></td>
    <td th:text="${user.name}"></td>
    </tr>
    </table>
  • th:value

    用来给标签赋值

    @GetMapping("/value")
    public ModelAndView value(){
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.setViewName("test");
    modelAndView.addObject("list","sprngboot");
    return modelAndView;
    }
    <!--value-->
    <input type="text" th:value="${list}">
  • th:src

    用来引入静态资源相当于HTML的原生的img。scrip的src标签

    图片、css、js都必须放在resource下的static中

    @GetMapping("/src")
    public ModelAndView src(){
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.setViewName("test");
    modelAndView.addObject("src","/1.jpg");
    return modelAndView;
    }
<!--value-->
<input type="text" th:value="${list}">
  • th:href

    用来设置超链接的href

    @GetMapping("/href")
    public ModelAndView href(){
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.setViewName("test");
    modelAndView.addObject("src","https://www.baidu.com");
    return modelAndView;
    }
    <!--href-->
    <a th:href="${src}">百度</a>
  • th:selected标签

    给html设置选中的元素,条件成立选中,条件不成立不选中

@GetMapping("/selected")
public ModelAndView selected(){
List<User> users = Arrays.asList(new User("张三",1),new User("李四",2),new User("王五",3));
ModelAndView modelAndView =new ModelAndView();
modelAndView.setViewName("test");
modelAndView.addObject("list",users);
modelAndView.addObject("name","李四");
return modelAndView;
}
<!--selected-->
<select>
<option
th:each="user:${list}"
th:value="${user.id}"
th:text="${user.name}"
th:selected="${user.name==name}"
></option>
</select>

结合th:each来使用,首次遍历list的集合来动态的创建元素,更具每次遍历出的user、name于业务数据中的name是否相等来决定是否要选择。

  • th:attr

    给HTML的任意标签来赋值

    @GetMapping("/attr")
    public ModelAndView attr(){
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.setViewName("test");
    modelAndView.addObject("attr","spring boot");
    return modelAndView;
    }
    <!--attr-->
    <input th:attr="value=${attr}"><br>
    <input th:value="${attr}">

SpringBoot 整合Thymeleaf 、Thymeleaf常用标签的更多相关文章

  1. SpringBoot入门系列(五)Thymeleaf的常用标签和用法

    前面介绍了Spring Boot 中的整合Thymeleaf .不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/16577 ...

  2. SpringBoot整合Redis实现常用功能

    SpringBoot整合Redis实现常用功能 建议大小伙们,在写业务的时候,提前画好流程图,思路会清晰很多. 文末有解决缓存穿透和击穿的通用工具类. 1 登陆功能 我想,登陆功能是每个项目必备的功能 ...

  3. 4.Thymeleaf的常用标签

    一.常用标签 二.foreach案例 1.创建项目 2. 创建Student.java package cn.kgc.pojo; /** * Created by Administrator on 2 ...

  4. springboot整合freemark,thymeleaf

    先在pom文件引入freemark,thymeleaf的依赖,thymeleaf的html文件放在Resource-templates-thymeleaf目录下,freekmarker的ftl文件放在 ...

  5. SpringBoot整合freemarker中自定义标签获取字典表的数据

    因为在前端要根据字典表中的数据去将1.2这些值转换成对应的文字解释 1.首先要创建一个类去实现 TemplateDirectiveModel 类 @Component public class Dic ...

  6. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  7. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  8. JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎

    上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建.运行与配置>,从上篇博客的内容我们不难看出SpringBoot的便捷.本篇博客我们继续在上篇博客的基础上来看一下Spri ...

  9. (二)springboot整合thymeleaf模板

    在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...

  10. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

随机推荐

  1. MySQL数据库:7、SQL常用查询语句

    Python基础之MySQL数据库 目录 Python基础之MySQL数据库 一.SQL语句常用查询方法 前期数据准备 1.基本查询 2.编写SQL语句的小技巧 3.查询之where筛选 3.1.功能 ...

  2. 腾讯云数据库SaaS致力于构建数据库分布式云,为更多更广的用户提供服务

    大数据时代,数据库 SaaS 是企业实现降本增效和业务创新的重要抓手.在腾讯全球数字生态大会数据库 SaaS 专场上,腾讯云发布了多项数据库 SaaS 产品能力升级,并重点分享了其在上云.日常运维.数 ...

  3. Tkinter根据屏幕分辨率最大化适应屏幕

    还不能够实现所有组件随分辨率自动变化 # 实现的是界面覆盖整个屏幕 from tkinter import * import win32api, win32con # 获取屏幕的分辨率 width = ...

  4. python文件名解析---从文件名获得分类类别

    python文件名解析-从文件名获得分类类别 python os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表.列表以字母顺序. listdir()方法 举例说明 lis ...

  5. JAVA学到方法写了一个四则运算计算器,请教一下有什么需要改进的

    package method; /* * 四则运算计算器 * */ import java.util.Scanner; public class Demo07 { public static void ...

  6. PyTorch复现LeNet-5手写识别学习笔记

    用PyTorch搭建LeNet-5手写识别 首先申明,这篇博客用于记录本人看完LeNet-5论文,并对其中的算法进行复现的记录,可以看成是学习笔记 这里只介绍复现的工作,如果想了解更多有关网络的细节, ...

  7. Django静态文件配置、form表单、request对象、连接数据库、ORM

    目录 静态文件配置 静态文件相关配置 1.接口前缀 浏览器停用缓存 2.接口前缀动态匹配 form表单 action 控制数据提交的地址 method 控制数据提交的方法 请求方法补充 get: 朝服 ...

  8. 利用WordPress搭建属于自己的网站

    怎么用WordPress给自己搭建了一个网站?可能很多人都想拥有属于自己的网站,这篇文章就找你怎么利用WordPress搭建属于自己的网站.如果你也正好有搭建个人网站的想法,那么本文会给你一个参考,我 ...

  9. MongoDB从入门到实战之Docker快速安装MongoDB

    前言 在上一篇文章中带领带同学们快速入门MongoDB这个文档型的NoSQL数据库,让大家快速的了解了MongoDB的基本概念.这一章开始我们就开始实战篇教程,为了快速把MongoDB使用起来我将会把 ...

  10. [Untiy]贪吃蛇大作战(四)——游戏主界面

    游戏主界面: 由于这个场景比较复杂,需要分几个部分实现: 1.游戏背景 首先我们的游戏场景上包括了一个大的背景图片,之外再包围一个红色的区域.中间的区域才是可活动的区域,周围通过碰撞检测盒来检测是否有 ...