SpringBoot零基础入门指南6--thymeleaf模板
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application-dev.yml中添加
#### thymeleaf
cache设置为false 界面不重启容器也能更新
#####
spring:
main:
banner-mode: "off" ## 启动banner图取消
thymeleaf:
cache: false
mode: HTML5
DataService.java添加
List<A> getAllData();
DataServiceImpl.java添加
@Override
public List<A> getAllData() {
AExample aExample = new AExample();
aExample.createCriteria().andIdBetween(1, 8);
List<A> list = aMapper.selectByExample(aExample);
return list;
}
controller文件夹下创建ViewController.java并添加
package com.xingquan.controller;
import com.xingquan.entity.A;
import com.xingquan.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ViewController {
@Autowired
private DataService dataService;
@RequestMapping("showall")
public ModelAndView getData() {
ModelAndView mav = new ModelAndView("view/database");
List<A> list = dataService.getAllData();
mav.addObject("data", list);
return mav;
}
}
在resources -templates下新建view文件夹并创建database.html文件并添加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>数据库数据显示</title>
</head>
<body>
<div>
进来了
</div>
<table>
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
</tr>
</thead>
<tbody>
<tr th:each="da: ${data}">
<td th:text="${da.id}"></td>
<td th:text="${da.name}"></td>
</tr>
</tbody>
</table>
</body>
</html>
启动后

模板语法详情:http://www.thymeleaf.org/
启动可能会报错:org.xml.sax.saxparseexception,原因是html页面的<meta>标签没有结束标签
<meta charset="UTF-8"> ——》 <meta charset="UTF-8"/>

浙公网安备 33010602011771号