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/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

carefree798

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值