1.创建的对象
package com.cc.springboot.entity;
public class User {
private String username;
// 1:女 2:男
private Integer gender;
public User() {
}
public User(String username, Integer gender) {
this.username = username;
this.gender = gender;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
}
2.controller
package com.cc.springboot.controller;
import com.cc.springboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/success")
public String hello(){
return "hello cc";
}
@RequestMapping("/study")
public String study(Map<String, Object> map){
List<User> userList=new ArrayList<>();
userList.add(new User("小梦",1));
userList.add(new User("小里",2));
userList.add(new User("小岚",1));
map.put("userList",userList);
// 1 :女 2:男
map.put("sex",19);
map.put("man",19);
return "study";
}
}
3.模板实现
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--th:if 如果值为true,则显示标签-->
<h3 th:if="${#lists.isEmpty(userList)}">userList是空的</h3>
<h3 th:if="not ${#lists.isEmpty(userList)}">userList不是空的</h3>
<h3 th:unless=" ${#lists.isEmpty(userList)}">userList不是空的</h3>
<hr />
<div th:switch="${sex}">
<p th:case="1"> 女</p>
<p th:case="${man}"> 男</p>
<!--若上面条件均不满足-->
<p th:case="*"> 未知</p>
</div>
</body>
</html>
关键-》(th:if th:unless) (th:switch th:case)