thymeleaf整合SSM报错:net::ERR_INCOMPLETE_CHUNKED_ENCODING (200)
1.问题引出:
在写SSM整合thymeleaf的时候,总是出现这样的错误。大多时候是发生在,从数据库查出来了数据,然后通过Model存储起来,跳转到前台页面,通过th:foreach循环遍历的时候就会出现这样的错误。案例如下:
后台代码:
//后台可以正确打印出来查询出来的theFriends列表数据,说明后台查询数据库没有问题
@RequestMapping("/findByUserId")
public String findByUserId(int userId, ModelMap modelMap)
{
List<User> theFriends = friendService.findByUserId(userId);
System.out.println("好友信息:"+theFriends);
//[User(userId=1, account=1, uname=1, upwd=123, age=24, sex=女),null]
modelMap.put("theFriends", theFriends);
return "/friendsPage";
}
前台代码:
<!-- [User(userId=1, account=1, uname=1, upwd=123, age=24, sex=女), null] -->
<!-- 同样前台Thymeleaf可以正确打印出来theFriends列表数据,说明问题出在了循环 -->
<p th:text="${theFriends}"></p>
<table>
<tr th:each="friend : ${theFriends}">
<td th:text="${friend.account}"></td>
<td th:text="${friend.uname}"></td>
<td th:text="${friend.age}"></td>
</tr>
</table>
问题分析:
问题就是在于查询出来的数据[User(userId=1, account=1, uname=1, upwd=123, age=24, sex=女), null],因为里面有一个对象值为null,当通过${friend.account}取数据的时候,就会有空指针异常,然而前端报错就是net::ERR_INCOMPLETE_CHUNKED_ENCODING
2.发生原因:
上面只是这个错误的其中一个原因,因为列表中有对象为null,所有会报错。其实这个错误,在我写前台thymeleaf的时候,屡屡发生。当然,最后发现原因都离不开thymeleaf前端标签的写错。汇总如下:
1.路径漏写了@{ }
<!--最多的情况就是地址漏写了@{ }-->
<img th:src="@{/svg-icons/back-to-top.svg}" alt="arrow" >
错误:<img th:src="/svg-icons/back-to-top.svg" alt="arrow" >
<a th:href="@{/profilePage(userId=${friend.userId})}" ></a>
错误:<a th:href="/profilePage(userId=${friend.userId})" ></a>
2.取参数${ }变量名的错误
${user.userId}
注意user对象里面有没有该变量名
${session.user.getUserId()}
通过session获取里面的user,获取userId是通过方法名的形式,最后的括号不要忘记了。
其他的一些,很多都是语法问题,多百度,针对于thymeleaf的地方多看看。
3.解决方法:
- 写代码的时候仔细一点,基本的变量名、单词不要写错,拼错
- 可以通过注释部分代码,然后看前台页面是否报错,然后找到发生错误的位置,再检查thymeleaf的语法有没有问题。