Spring Boot模板引擎Thymeleaf demo

Posted weixin_45253622

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot模板引擎Thymeleaf demo相关的知识,希望对你有一定的参考价值。

Thymeleaf 依赖
pom.xml

        <!--thymeleaf模板引擎配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

创建模板文件

resources->templates下新建html文件
thymeleaf.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">     <!--引入命名空间-->
<head>
    <meta charset="UTF-8">
    <title>thymeleaf demo</title>
</head>
<body>
<p>description字段值为:</p>
<p th:text="$description">这里显示的是description字段值</p>
</body>
</html>

resources->templates->admin
attributes.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf setting-value-to-specific-attributes</title>
</head>
<body>

<h1 th:text="$title">标签演示</h1>
<div>
    <h5>id name value标签:</h5>
    <input id="input1" name="input1" value="1" th:id="$th_id" th:name="$th_name" th:value="$th_value"/>
</div>

<div class="div1" th:class="$th_class">
    <h5>class href 标签:</h5>
    <a href="##" th:href="$th_href">链接地址</a>
</div>

</body>
</html>

resources->templates
simple.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf simple syntax</title>
</head>
<body>
<h1>Thymeleaf 简单语法</h1>

<div>
    <h5>字符串</h5>
    <p>一个简单的字符串:<span th:text="thymeleaftext">default text</span></p>
    <p>字符串的拼接:<span th:text="'thymeleaf text concat,'+$Text">default text</span></p>
    <p>字符串的拼接2:<span th:text="|thymeleaf text concat,$Text|">default text</span></p>
</div>

<div>
    <h5>数字</h5>
    <p>一个简单的数字:<span th:text="2022">1</span></p>
    <p>加法运算:2019+1=<span th:text="$number1+1">1</span></p>
    <p>减法运算:14-1=<span th:text="14-1">default</span></p>
    <p>乘法运算:1010×2=<span th:text="$number2*2">1</span></p>
    <p>除法运算:39÷3=<span th:text="39/3">1</span></p>
</div>

<div>
    <h5>布尔</h5>
    <p>数字比较:2020>2019=<span th:text="2020>2019">default</span></p>
    <p>数字比较:2020 gt 2019=<span th:text="2020 gt 2019">default</span></p>
    <p>数字比较:2020 lt 2019=<span th:text="2020 lt 2019">default</span></p>
    <p>数字比较:13 == 39/3,结果为<span th:text="13 == 39/3">default</span></p>
    <p>字符串比较:thymeleafText == 'csdn-user',结果为:<span th:text="$Text=='csdn-user'">default</span></p>
</div>

</body>
</html>

controller包中
新建ThymeleafController.java

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ThymeleafController 

    @GetMapping("/thymeleaf")
    public String hello(HttpServletRequest request,@RequestParam(value = "description",required = false,defaultValue = "默认值") String description)
        request.setAttribute("description","传值为"+description);        //把变量放到request域中
        return "thymeleaf";
    

    @GetMapping("/attributes")
    public String attributes(HttpServletRequest request)
        request.setAttribute("title","thymeleaf标签演示");
        request.setAttribute("th_id","thymeleaf-input");
        request.setAttribute("th_name","thymeleaf-name");
        request.setAttribute("th_value","thymeleaf-value");
        request.setAttribute("th_class","thymeleaf-class");
        request.setAttribute("th_href","https://www.baidu.com");
        return "/admin/attributes";
    
    @GetMapping("/simple")
    public String simple(HttpServletRequest request)
        request.setAttribute("Text","csdn-user");
        request.setAttribute("number1","2019");
        request.setAttribute("number2","1010");
        return "simple";
    


访问:http://localhost:8080/thymeleaf

访问:http://localhost:8080/thymeleaf?description=ch1


访问:http://localhost:8080/attributes

访问:http://localhost:8080/simple

学习参考:
https://edu.csdn.net/learn/26258/328801
https://www.thymeleaf.org/

以上是关于Spring Boot模板引擎Thymeleaf demo的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot模板引擎Thymeleaf demo

Spring Boot模板引擎Thymeleaf demo

Spring Boot2:使用Spring Boot结合Thymeleaf模板引擎使用总结

Spring Boot Thymeleaf 模板引擎的使用

Spring Boot 2.0 整合Thymeleaf 模板引擎

Spring Boot系列——模板引擎thymeleaf