• 22.在springboot中使用thymeleaf模板(第一个例子)


    一、认识 Thymeleaf

          Thymeleaf 是一个流行的模板引擎,该模板引擎采用 Java 语言开发。
          模板引擎是一个技术名词,是跨领域跨平台的概念,在 Java 语言体系下有模板引擎,在C# PHP 语言体系下也有模板引擎,甚至在 JavaScript 中也会用到模板引擎技术, Java 生态下 的模板引擎有 Thymeleaf Freemaker Velocity Beetl (国产) 等。
           Thymeleaf 对网络环境不存在严格的要求,既能用于 Web 环境下,也能用于非 Web 环境 下。在非 Web 环境下,他能直接显示模板上的静态数据;在 Web 环境下,它能像 Jsp 一样从 后台接收数据并替换掉模板上的静态数据。它是基于 HTML 的,以 HTML 标签为载体, Thymeleaf 要寄托在 HTML 标签下实现。
           Spring Boot 集成了 Thymeleaf 模板技术,并且 Spring Boot 官方也推荐使用 Thymeleaf 替代 JSP 技术, Thymeleaf 是另外的一种模板技术,它本身并不属于 Spring Boot Spring Boot 只是很好地集成这种模板技术,作为前端页面的数据展示,在过去的 Java Web 开发中,我们 往往会选择使用 Jsp 去完成页面的动态渲染,但是 jsp 需要翻译编译运行,效率低
    Thymeleaf 的官方网站: http://www.thymeleaf.org
    Thymeleaf 官方手册: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

    二、第一个实例

    项目结构

     

    1.创建springboot项目添加web起步依赖和thymeleaf依赖。

     pom.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.3version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.itgroupId>
    12. <artifactId>027-thymeleaf-firstartifactId>
    13. <version>1.0.0version>
    14. <properties>
    15. <java.version>1.8java.version>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>org.springframework.bootgroupId>
    20. <artifactId>spring-boot-starter-thymeleafartifactId>
    21. dependency>
    22. <dependency>
    23. <groupId>org.springframework.bootgroupId>
    24. <artifactId>spring-boot-starter-webartifactId>
    25. dependency>
    26. <dependency>
    27. <groupId>org.springframework.bootgroupId>
    28. <artifactId>spring-boot-starter-testartifactId>
    29. <scope>testscope>
    30. dependency>
    31. dependencies>
    32. <build>
    33. <plugins>
    34. <plugin>
    35. <groupId>org.springframework.bootgroupId>
    36. <artifactId>spring-boot-maven-pluginartifactId>
    37. plugin>
    38. plugins>
    39. build>
    40. project>

    2.创建controller包下的HelloThymeleafController类

    1. package com.it.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.ui.Model;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. @Controller
    7. public class HelloThymeleafController {
    8. @GetMapping("/hello")
    9. public String helloThymeleaf(Model model,HttpServletRequest httpServletRequest){
    10. //添加数据到request作用域,模板引擎可以从request中获取数据
    11. httpServletRequest.setAttribute("data","欢迎使用thymeleaf模板引擎");
    12. model.addAttribute("mydata","model中的数据");
    13. //指定视图(模板引用使用的页面html)
    14. return "hello";
    15. }
    16. }

    3.hello.html文件

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>hello.htmltitle>
    6. head>
    7. <body>
    8. <h3>使用模板thymeleaf的例子h3>
    9. <p th:text="${data}">想显示数据p>
    10. <p th:text="${mydata}">显示数据p>
    11. body>
    12. html>

    4.进入主函数入口类启动项目进行测试

    测试结果中使用了thymeleaf模板的标签,这些标签中的数据会被request传递过来的数据代替,以实现数据更新。

     

  • 相关阅读:
    gitlab CI/CD 自动化部署vue项目到阿里云服务器步骤
    Effective C++ 阅读笔记 03:资源管理
    uni-app之android离线打包
    3月黄油奶酪行业数据分析:安佳和妙可蓝多领军市场
    目标检测算法改进系列之Backbone替换为LSKNet
    【JavaEE初阶】前端篇:CSS(下篇)
    【git常用命令以及工作中都怎么工作】
    js中数组去重(数组中元素是对象)
    【Python】Python常用的日期时间函数
    全栈工程师需要具备哪些技能?
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/126746061