配置
在application.properties文件中增加Thymeleaf模板的配置。
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
更多参考https://blog.csdn.net/ncc1995/article/details/100019867
获取数据
简单表达式
${...} 变量表达式
*{...} 选择变量表达式
#{...} 消息表达式
@{...} 链接url表达式
1.一般变量的取值:
<h1 th:text="${name}"></h1>
2.从对象中取值:
<input type="text" name="name" value="James Carrot" th:value="${user.name}" />
3,list循环迭代
<ul>
<li th:each="item: ${list}" th:text="${item}"></li>
</ul>
更多参考
https://blog.csdn.net/qq_22860341/article/details/79229181
前端从对象中获得属性实例
controller中
@Controller
public class TestString {
// 这是测试controller 与restController返回的是视图名还是字符串
@RequestMapping("/test6")
public String getcontroller(Model model) {
Person person = new Person("aa",11);
List<Person> people = new ArrayList<Person>();
Person p1 = new Person("xxx",12);
Person p2 = new Person("yyy",14);
Person p3 = new Person("zzz",13);
people.add(p1);
people.add(p2);
people.add(p3);
model.addAttribute("singleperson",person);
model.addAttribute("people",people);
return "test";
}
}
HTML中
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<div class="panel-body">
<span th:text="${singleperson.name}"></span>
</div>
<ul class="list-group">
<li class="list-group-item" th:each="person:${people}">
<span th:text="${person.name}"></span>
<span th:text="${person.age}"></span>
<button class="btn" th:οnclick="'getName(\'' + ${person.name} + '\');'">获得名字</button>
</li>
</ul>
</body>
</html>
显示效果
前端从List中获取数据
看上面的代码
html代码参考
<ul class="list-group">
<li class="list-group-item" th:each="person:${people}">
<span th:text="${person.name}"></span>
<span th:text="${person.age}"></span>
<button class="btn" th:οnclick="'getName(\'' + ${person.name} + '\');'">获得名字</button>
</li>
</ul>
前端从Map中获取数据
controller中
@Controller
public class TestString {
// 这是测试controller 与restController返回的是视图名还是字符串
@RequestMapping("/test6")
public String getcontroller(Model model) {
Person person = new Person("aa",11);
List<Person> people = new ArrayList<Person>();
Person p1 = new Person("xxx",12);
Person p2 = new Person("yyy",14);
Person p3 = new Person("zzz",13);
people.add(p1);
people.add(p2);
people.add(p3);
Map<String,Person> map = new HashMap<String, Person>();
map.put("aa",new Person("bb",11));
map.put("bbb",new Person("BBBB",13));
map.put("ccc",new Person("DDD",14));
model.addAttribute("map1",map);
model.addAttribute("singleperson",person);
model.addAttribute("people",people);
return "test";
}
}
html中
<table border="1" >
<tr th:each="userEntry,userStat:${map1}">
<td th:text="${userStat.index}"></td>
<td th:text="${userStat}"></td>
<td th:text="${userEntry}"></td>
<td th:text="${userEntry.value}"></td>
<td th:text="${userEntry.value.name}"></td>
<td th:text="${userEntry.value.age}"></td>
</tr>
</table>
效果展示
参考