springboot整合thymeleaf

目录

一、介绍

二、Thymeleaf 的特点

三、语法规则

四、springboot中集成Thymeleaf

五、使用

六、将数据存入页面并取出

   6.1 存入普通数据

        6.1.2 存入对象

        6.1.3 存入List

6.2 跳转另一个页面

6.3 判断

写在最后


一、介绍

        Thymeleaf 是新一代 Java 模板引擎,Thymeleaf 支持 HTML 原型,其文件后缀为“.html”,因此它可以直接被浏览器打开,此时浏览器会忽略未定义的 Thymeleaf 标签属性,展示 thymeleaf 模板的静态页面效果;当通过 Web 应用程序访问时,Thymeleaf 会动态地替换掉静态内容,使页面动态显示。

二、Thymeleaf 的特点

 

动静结合、开箱即用、多方言支持等

三、语法规则

在使用 Thymeleaf 之前,首先要在页面的 html 标签中声明名称空间

xmlns:th="http://www.thymeleaf.org"

Thymeleaf 语法分为以下 2 类:

  • 标准表达式语法

                变量表达式:${...}

                选择变量表达式:*{...}

                链接表达式:@{...}

                国际化表达式:#{...}

                片段引用表达式:~{...}等

  • th 属性

          th:id 、 th:text、 th:utext、 th:object、 th:each、  th:if 等等、用来替换原有的html属性。  

四、springboot中集成Thymeleaf

        1、导入依赖

        

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>

        2、配置

spring:
  thymeleaf:
    mode: HTML
    cache: false

五、使用

  1、在resource目录下新建templates、存放页面资源

        index.html如下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>hello,Thymeleaf</h1>
</body>
</html>

2、在IndexController,新建一个方法,拦截Get请求,拦截路劲为"/index",如下

 代码:

@Controller
public class IndexController {

    @GetMapping("/index")
    public String index(){
        return "index";
    }

}

启动后访问改路径:http://localhost:8889/index

 如此、便是测试成功了

六、将数据存入页面并取出

        6.1 存入普通数据

 6.1.1 存入普通数据 msg

    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("msg","hello ,springboot");
        return "index";
    }

 取出msg

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>hello,Thymeleaf</h1>

    <h1 th:text="${msg}"></h1>
    <h1 th:utext="${msg}"></h1>

</body>
</html>

访问页面后的结果

6.1.2 存入对象

6.1.2 存入对象

 @Controller
public class IndexController {

    @GetMapping("/index")
    public String index(Model model){
        IUser user = new IUser();
        user.setUserName("张三");
        user.setEmail("123456@qq.com");
        user.setPassWord("123");
        model.addAttribute("user",user);
    }

}

取出存入对象

<h1 th:utext="${user}"></h1>

测试

 取出存入对象的某个值

<div th:object="${user}">
    <h1 th:text="${user.userName}"></h1>
    <h1 th:text="${user.passWord}"></h1>
</div>

测试

6.1.3 存入List

6.1.3 存入List

    @GetMapping("/index")
    public String index(Model model){
//        model.addAttribute("msg","hello ,springboot");

        IUser user1 = new IUser();
        user1.setUserName("张三");
        user1.setEmail("123456@qq.com");
        user1.setPassWord("123");

        IUser user2 = new IUser();
        user2.setUserName("张四");
        user2.setEmail("123456@qq.com");
        user2.setPassWord("1234");

        IUser user3 = new IUser();
        user3.setUserName("张五");
        user3.setEmail("123456@qq.com");
        user3.setPassWord("12345");

      List<IUser> userList = new ArrayList<>();
      userList.add(user1);
      userList.add(user2);
      userList.add(user3);
      model.addAttribute("userList",userList);

        return "index";
    }

遍历List并取出1

<h1 th:each="user :${userList}" th:text="${user}" ></h1>

测试

 遍历List并取出2

<div th:each="user :${userList}" >
            <h1 th:text="${user}"></h1>
</div>

测试

 遍历List并取出3

<div th:each="user :${userList}" >
           [[${user}]]
</div>

测试:

 

遍历userList,得到user,取出user的某个属性的值,

直接通过以属性的方式获取即可,例如:

<div th:each="user :${userList}" >
           [[${user.userName}]]
</div>

 测试

6.2 跳转另一个页面

准备:

    
    @GetMapping("/getUser")
    @ResponseBody
    private String getUser(){
        return "getUserDate";
    }

模拟下,访问此接口,得到User的所有数据并转为Json格式

跳转链接:

<a th:href="@{/getUser}">Thymeleaf写法:查询用户</a>
<hr>
<a href="/getUser">原生写法:查询用户</a>

 两种方法否可以实现跳转:

6.3 判断

  @GetMapping("/index")
    public String index(Model model){
        int a =1;
        model.addAttribute("msg",a);

        return "index";
    }

将int a =1,存入model对象,页面取出后进行判断

<div th:if="${msg<1}">
    <h1>a<1</h1>
</div>

<div th:unless="${msg>2}">
    <h1>a<=2</h1>
</div>

 基于可以根据条件展示不同结果、按钮等等

Spring Boot整合Thymeleaf是一种常见的做法,用于在Spring Boot应用中利用Thymeleaf作为模板引擎,提供动态网页功能。Thymeleaf是一个强大的、现代的Web模板引擎,支持HTML5和XML。 以下是整合步骤: 1. 添加依赖:在你的`pom.xml`文件中添加Thymeleaf及其Spring Boot支持的依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> ``` 2. 配置视图解析器:在`application.properties`或`application.yml`中设置Thymeleaf的视图位置: ``` spring.thymeleaf.views.location=classpath:/templates/ ``` 3. 创建模板目录:在项目的`src/main/resources/templates`目录下创建HTML模板文件。 4. 使用Thymeleaf标签:在模板文件中,你可以使用Thymeleaf的表达式语言(EL)和特殊语法,如条件语句、迭代等。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>My Spring Boot App</title> </head> <body> <h1 th:text="${message}">Hello, World!</h1> </body> </html> ``` 5. 在Controller中返回模型数据并指定视图:例如: ```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Welcome to Spring Boot with Thymeleaf!"); return "home"; // 指定模板名称 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值