学习中,在使用thymeleaf中,出现的问题
数据库是Account,字段为账户aid ,账户余额money,用户编号uid
Controller层 AccountController
@Controller
@RequestMapping("/acc")
public class AccountController {
@Autowired
private AccountService accountService;
@GetMapping("/ac")
public String query(ModelMap modelMap){
List<Account> accounts = accountService.accounts();
modelMap.put("accounts",accounts);
return "account-list";
}
}
位于src/main/resources/templates文件下的account-list.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" th:href="@{../css/style.css}"/>
<script src="" th:src="@{../js/fu.js}"></script>
</head>
<body>
<img th:src="@{../images/wode.jpg}" class="wode"/>
<h1>用户列表展示</h1>
<table border="1">
<tr>
<th>序号</th>
<th>账户编号</th>
<th>账户余额</th>
<th>用户编号</th>
</tr>
<tr th:each="account,iterStat:${accounts}">
<td th:text="${iterStat.index+1}"></td>
<td th:text=""></td>
<td th:text="${account.aid}"></td>
<td th:text="${account.money}"></td>
<td th:text="${account.uid}"></td>
</tr>
</table>
<hr/>
<br>
<button onclick="text()">点击测试</button>
</body>
</html>
访问localhost:8092/acc/ac出错,端口号我改过
解决:
1.注意不要+@RestController,否则下面返回的就不是一个页面,而是字符串了
2.thymeleaf语法严格,页面引用不完整或者语法有问题,或者没写值,都会报错导致页面不显示
3.如果页面静态资源没加载出来,那就是资源引入路径的问题
<script src="" th:src="@{../js/fu.js}"></script>
看文件报错在哪,找到这一行,说在22行有问题,有一个空值
在这里有一个空值,把空值注释掉
运行,完美可以了