1 编写application.properties文件
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html #springboot 官方文档建议我们关闭thymeleaf的缓存 spring.thymeleaf.cache=false
2.创建实体类
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; } public Student(){ } }
3 创建Controller层
@RequestMapping("/getStudents") public String getStudents(Model model){ System.out.println("hello"); List<Student> studentList=new ArrayList<>(); Student student1=new Student(111,"张三"); Student student2=new Student(222,"李四"); Student student3=new Student(333,"王五"); studentList.add(student1); studentList.add(student2); studentList.add(student3); model.addAttribute("student",studentList); return "Hello"; }
4.编写html页面
<body> <table border="1"> <tr> <td>学生编号</td> <td>学生姓名</td> </tr> <tr th:each="stu:${student}"> <td th:text="${stu.stu_id}"></td> <td th:text="${stu.stu_name}"></td> </tr> </table> </body>
5.启动程序
@SpringBootApplication public class StartSpringBoot { public static void main(String[] args) { SpringApplication.run(StartSpringBoot.class,args); } }
6.运行 结果