
然后再把【application.properties】弄成这个样子:
#thymeleaf 配置
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
#缓存设置为false, 这样修改之后马上生效,便于调试
spring.thymeleaf.cache=false
然后重启项目,对我们的hello.html稍稍做一些修改,稍等一会儿,你就能刷新页面看到效果,完美。
Thymeleaf 的迭代和 JSP 的写法也很相似,我们将就上面的hello项目改一下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Thymeleaf快速入门-Hello Thymeleaf</title>
</head>
<body>
<table>
<thead>
<tr>
<th>学生id</th>
<th>学生姓名</th>
</tr>
</thead>
<tbody>
<tr th:each="s:${students}">
<td th:text="${s.id}"></td>
<td th:text="${s.name}"></td>
</tr>
</tbody>
</table>
</body>
</html>
为了配合演示,在【com.wmyskxz.demo】下新建一个【pojo】包,然后新建一个【Student】类:
package com.wmyskxz.demo.pojo;
public class Student {
private String name;
private Integer id;
public Student(String name, Integer id) {
this.name = name;
this.id = id;
}
// getter and setter
}
再把controller改改,给前端添加几条数据:
@RequestMapping("/hello")
public String hello(Model model) {
List<Student> students = new ArrayList<>();
students.add(new Student("张三", 1));
students.add(new Student("李四", 2));
students.add(new Student("王五", 3));
students.add(new Student("二麻子", 4));
students.add(new Student("三棒子", 5));
model.addAttribute("students", students);
return "hello";
}
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-91180-5.html
好期待台宣独
为什么韩国出专辑一定要把头发弄的五颜六色
质检总局是怕人家说他们只拿钱不干活儿