• Spring Boot——Thymeleaf


    ???

    哈喽!大家好,我是【】,一位上进心十足的【Java领域博主】!???

    【】的写作风格:喜欢用【通俗易懂】的文笔去讲解每一个知识点,而不喜欢用【高大上】的官方陈述。

    【】博客的领域是【面向后端技术】的学习,未来会持续更新更多的【后端技术】以及【学习心得】。

    如果有对【后端技术】感兴趣的【小可爱】,欢迎关注【】???

    感谢各位大可爱小可爱!


    目录

    一、背景

    二、Thymeleaf

    2.1特点

    2.2使用

    三、Thymeleaf语法

    3.1 ${}: 标准变量表达式

    3.2 选择变量表达式?*{} 和 th:object

    3.3 链接(URL)表达式 和 th:href

    3.4 th标签之th:action

    3.5 th标签之th:each

    3.6 th标签之th:switch/th:case

    结语


    一、背景

    我们之前开发,我们需要将前端转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。

    jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先是以jar的方式,不是war,其二,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。

    那该怎么办呢?

    SpringBoot推荐我们可以来使用模板引擎。

    什么是模板引擎?

    模板引擎的作用就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,就是我们在后台封装一些数据。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终生成一个我们想要的内容给我们写出去,这就是我们这个模板引擎,不管是jsp还是其他模板引擎,都是这个思想。

    SpringBoot给我们推荐的模板引擎就是Thymeleaf,这模板引擎是一个高级语言的模板引擎,他的这个语法更简单,而且功能更强大

    二、Thymeleaf

    2.1特点

    (1)thymeleaf模板引擎既能用于web环境下,也能用于非web环境下,在非web环境下,它能直接显示模板上的静态数据,在web环境下,它能像jsp一样从后台接收数据并替换掉模板上的静态数据。

    (2)thymeleaf是基于html的,以html标签为载体,thymeleaf要寄托在html的标签下实现对数据的展示。

    2.2使用

    Thymeleaf的使用非常简单,只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。

    (1)导入依赖

      
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    
    • 1
    • 2
    • 3
    • 4

    (2)在resources下建立一个目录templates

    (3)编写test.html

    
    
    
        
        Title
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (4)编写 Controller类

    package com.yixin.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MyController {
        @RequestMapping("/test")
        public String test1(){
            return "test";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (5)运行

    测试成功!说明成功访问到了templates目录。

    三、Thymeleaf语法

    由于Thymeleaf的语法太多了,只在这里讲几个常见的语法,对于其它的语法可以前往官网进行查阅

    官网:Thymeleaf

    常见的语法

    • ${}: 标准变量表达式
    • 选择变量表达式*{} 和 th:object
    • 链接(URL)表达式 和 th:href
    • th标签之th:action
    • th标签之th:each
    • th标签之th:switch/th:case

    前提:

    导入thymeleaf的名称空间

    
    
    • 1

    3.1 ${}: 标准变量表达式

    Controller:

    @RequestMapping("/test2")
    public String test2(Model model) {
    
            model.addAttribute("msg", "标准变量表达式");
    
            Blog blog=new Blog();
            blog.setId(1);
            blog.setName("yixin");
            blog.setPwd("123");
    
            model.addAttribute("blog",blog);
            return "test";
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    test.html:

    
    
    
        
        Title
    
    
    
    
    span默认文本内容

    id: xx name: xxx pwd: xxx
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    运行:

    3.2 选择变量表达式*{} 和 th:object

    *{}: 选择变量表达式

    标准变量表达式和选择变量表达式可以混合使用 ;

    先用 th:object来绑定 blog 对象, 然后用 * 来代表这个 blog对象

    
    
    
        
        Title
    
    
    
    span默认文本内容
    id: xxx name: xxx age: xxx
    id: xxx name: xxx age: xxx
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    运行:

    3.3 链接(URL)表达式 和 th:href

    使用说明:

    URL表达式

    语法:@{…}

    URL表达式可用于

    (1)绝对URL,比如:

    查看

    (2)相对URL,相对于页面,比如:

    查看

    (3)相对于URL,相对于项目上下文,比如:

    查看(项目的上下文名会被自动添加)

    Controller类:

    @RequestMapping("/test2")
    public String test2(Model model) {
            model.addAttribute("msg", "标准变量表达式");
    
            Blog blog=new Blog();
            blog.setId(1);
            blog.setName("yixin");
            blog.setPwd("123");
    
            model.addAttribute("blog",blog);
            return "test3";
            }
    
    @RequestMapping("/blog")
    @ResponseBody
    public String getUserById(Integer id) {
    
            System.out.println("id=" + id);
            return "id=" + id;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    test3.html:

    
    
    
     
     Title
    
    
    博客id
    博客id
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行:

    3.4 th标签之th:action

    Controller类:

    @RequestMapping("/test2")
    public String test2(Model model) {
    
            model.addAttribute("msg", "标准变量表达式");
            Blog blog=new Blog();
            blog.setId(1);
            blog.setName("yixin");
            blog.setPwd("123");
            model.addAttribute("blog",blog);
    
            return "test4";
            }
    
    @RequestMapping("/blog")
    @ResponseBody
    public String getUserById(Integer id) {
            System.out.println("id=" + id);
            return "id=" + id;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    test4.html:

    
    
    
     
     Title
    
    
    
    id:
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    运行:

    3.5 th标签之th:each

    Controller类:

    @RequestMapping("/test2")
    public String test2(Model model) {
    
            model.addAttribute("msg", "标准变量表达式");
            Blog blog=new Blog();
            blog.setId(1);
            blog.setName("yixin");
            blog.setPwd("123");
    
            model.addAttribute("blog",blog);
            return "test4";
            }
    
    @RequestMapping("/blogList")
    public String hello(Model model) {
            List blogList = new ArrayList<>();
    
            for (int i = 1; i <= 3; i++) {
            Blog blog=new Blog();
            blog.setId(i);
            blog.setPwd("abcd"+i);
            blog.setName("一心"+i);
            blogList.add(blog);
            }
    
            model.addAttribute("blogList", blogList);
            return "test5";
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    test5.html:

    
    
    
     
     Title
    
    
    
     

    xxx xxx xxx

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    运行:

    3.6 th标签之th:switch/th:case

    Controller类:

    @RequestMapping("/test2")
    public String test2(Model model) {
    
            model.addAttribute("msg", "标准变量表达式");
    
            Blog blog=new Blog();
            blog.setId(1);
            blog.setName("yixin");
            blog.setPwd("123");
    
            model.addAttribute("blog",blog);
            return "test6";
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    test6.html:

    
    
    
     
     Title
    
    
    
    
     昵称:
     
     一心
     张三
     
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    运行:


    结语

    以上就是【】对Thymeleaf的知识点和基本使用的讲解了,对于Thymeleaf这个模板引擎,说实话还是非常好用的,甚至个人觉得比jsp还强大,大家也可以自己亲手敲一遍代码,这样对知识的巩固有很大帮助,如果文章中有哪些地方讲得不是很好,欢迎大家提出来,让我们共同进步。

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    MyBatis Plus详细教程
    java.util.IllegalFormatConversionException: f != java.lang.String 问题解决!
    颜色模型(color model)
    RK3588 RK628D调试HDMI-IN(一)
    【我的前端】CSS启示录:CSS写出超级美观的阴影效果
    校招能成功上岸阿里、百度测试岗都是什么样的人~
    android分区概述
    游戏思考18:AOI视野同步算法介绍和简单实现(未完待续8/3)
    可靠的自托管「GitHub 热点速览 v.22.37」
    leetcode 399 除法求值
  • 原文地址:https://blog.csdn.net/m0_54849806/article/details/126114098