SpringBoot整合thymeleaf模板

一、目录展示

   

二、导入依赖

    <!-- 添加thymeleaf模版的依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  </dependencies>
View Code

  在properties节点中增加,因为thymeleaf 模版需要html页面的所有元素都自身闭合!3.0之后的版本不在需要! 

  <thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>

  <thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>

三、application.properties配置文件

  

 四、Student实体类

  

 五、ThymeleafController

package com.zn.controller;

import com.zn.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
    @RequestMapping("/FirstThymeleaf")
    public String FirstThymeleaf(Model model){
        System.out.println("成功进入ThymeleafController中的第一个方法呀");
        List<Student> list=new ArrayList<>();
        Student stu1=new Student(1,"张三");
        Student stu2=new Student(2,"李四");

        list.add(stu1);
        list.add(stu2);
        model.addAttribute("list",list);
        return "thymeleaf";

    }

}
View Code

六、thymeleaf.html页面

  

 七、测试类

  

八、效果展示

  

原文地址:https://www.cnblogs.com/Zzzzn/p/12029553.html