一.显示变量
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>info</title>
</head>
<body>
学号:<span th:text="${person.id+1}">0000</span><br />
姓名:<span th:text="${person.name}">0000</span><br />
性别:<span th:text="${person.sex?'男':'女'}">0000</span><br />
</body>
</html>
要求Person类里面属性是public的,或者有Getter方法。
@Controller
public class TestController
{
@RequestMapping("info.do")
public String myTest(Model model)
{
Person p=new Person(100,"lisong",true);
model.addAttribute("person",p);
return "info";//全路径 /template/info.html,相关配置在spring-mvc.xml里
}
}
二.显示Map
@Controller
public class TestController
{
@RequestMapping("info.do")
public String myTest(Model model)
{
Map<String, Object> person=new HashMap<String,Object>();
person.put("id", 100);
person.put("name", "lisong");
person.put("sex", true);
model.addAttribute("person",person);
return "info";//全路径 /template/info.html,相关配置在spring-mvc.xml里
}
}
后端使用同上
三.显示List
@Controller
public class TestController
{
@RequestMapping("info.do")
public String myTest(Model model)
{
List<Person> p=new ArrayList<Person>();
p.add(new Person(100,"lihua",true));
p.add(new Person(101,"liuming",true));
p.add(new Person(102,"xupan",false));
model.addAttribute("personList",p);
return "info";//全路径 /template/info.html,相关配置在spring-mvc.xml里
}
}
<table>
<tr th:each="row,start : ${personList}">
<td th:text="${start.index+1}">000</td>
<td th:text="${row.id}">000</td>
<td th:text="${row.name}">000</td>
<td th:text="${row.sex?'男':'女'}">000</td>
</tr>
</table>
row : 表示这一行
stat : 表示上下文,比如 stat.index 表示当前遍历索引