标签:return getname 操作 xmlns tps strip XML 约束 rip
thymeleaf也是一模板技术,与freemarker类似,本文讲述springboot如何整合thymeleaf。
在dependencies标签中加入以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在entity下创建User.java
package net.wanho.springboot.thymeleaf.entity;
public class User {
private Integer id;
private String name;
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在controller下创建UserController.java
package net.wanho.springboot.thymeleaf.controller;
import net.wanho.springboot.thymeleaf.entity.User;
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("/user")
public class UserController {
@RequestMapping("/list")
public String listUser(Model model) {
List<User> userList = new ArrayList<User>();
for (int i = 0; i <10; i++) {
userList.add(new User(i,"ali "+i));
}
model.addAttribute("users", userList);
return "/user/list";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
<link rel="stylesheet" href="/css/bootstrap.min.3.3.5.css"/>
<script src="/js/jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="/js/bootstrap.js" type="text/javascript"></script>
</head>
<body>
<div>
<table class="table">
<caption>用户基本信息</caption>
<thead>
<tr>
<th>id</th>
<th>姓名</th>
</tr>
</thead>
<tbody th:each="user:${users}">
<tr>
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
在浏览器中输入http://localhost:8080/user/list,查看效果,具体情况如下:
至此,我们完成了springboot对thymeleaf的支持,并完成了测试。
标签:return getname 操作 xmlns tps strip XML 约束 rip
原文地址:https://www.cnblogs.com/alichengxuyuan/p/12581500.html