SpringBoot2
Posted 寒冷雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot2相关的知识,希望对你有一定的参考价值。
一、thymeleaf模板
1、在pom.xml中导入依赖
<!-- 添加thymeleaf模版的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2、配置application.yml文件
###配置thymeleaf spring: thymeleaf: cache: false
3、在resources目录下添加templates包,并在此包下创建index.html页面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>thymeleaf模板页面</title> </head> <body> <ul th:each="stu:${stuList}"> <li> <span th:text="${stu.stu_id}"></span> <span th:text="${stu.stu_name}"></span> </li> </ul> </body> </html>
4、创建启动类
package com.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class StartSpringBoot { public static void main(String[] args){ SpringApplication.run(StartSpringBoot.class,args); } }
5、创建实体类student
package com.boot.entity; public class student { private Integer stu_id; private String stu_name; public Integer getStu_id() { return stu_id; } public void setStu_id(Integer stu_id) { this.stu_id = stu_id; } public String getStu_name() { return stu_name; } public void setStu_name(String stu_name) { this.stu_name = stu_name; } public student(Integer stu_id, String stu_name) { this.stu_id = stu_id; this.stu_name = stu_name; } }
6、创建Controller类
package com.boot.controller; import com.boot.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("/thymeleafController") public class thymeleafController { @RequestMapping("/thymeleaf") public String thymeleaf(Model model){ List<student> list=new ArrayList<>(); student stu1=new student(1,"张三"); student stu2=new student(2,"李四"); student stu3=new student(3,"王五"); list.add(stu1); list.add(stu2); list.add(stu3); model.addAttribute("stuList",list); return "index"; } }
以上是关于SpringBoot2的主要内容,如果未能解决你的问题,请参考以下文章