SpringBoot+thymeleaf出现SpelEvaluationException: EL1007E: Property or field ‘data‘ canno be found on n
今早打开昨天的项目,加了一个小功能,然后项目就崩了。主要是这个功能和这里一点关系都没有。
明明昨天晚上睡觉前都还好好的,今天一打开连首页都访问不了。崩溃。
花了一个多小时解决完这个问题,复盘错误时,把它还原成出错时的样子时它竟然又好了。我怀疑它在搞我心态。编程是门玄学问题。
来看看这个奇奇怪怪的bug吧
网页报错500

控制台:
```org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates//index.html]")
再看详细错误信息:
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "map.user.headerUrl" (template: "/index" - line 123, col 12)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 48 common frames omitted
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "map.user.headerUrl" (template: "/index" - line 123, col 12)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'headerUrl' cannot be found on null
这是按照提示找到的index.html中出错的代码
<!-- 帖子列表 -->
<ul class="list-unstyled">
<li class="media pb-3 pt-3 mb-3 border-bottom" th:each="map:${discussPosts}">
<a href="site/profile.html">
<img th:src="${map.user.headerUrl}" class="mr-4 rounded-circle" alt="用户头像" style="width:50px;height:50px;">
</a>
<div class="media-body">
<h6 class="mt-0 mb-3">
<a href="#" th:utext="${map.post.title}">备战春招,面试刷题跟他复习,一个月全搞定!</a>
<span class="badge badge-secondary bg-primary" th:if="${map.post.type==1}">置顶</span>
<span class="badge badge-secondary bg-danger" th:if="${map.post.status==1}">精华</span>
</h6>
<div class="text-muted font-size-12">
<u class="mr-3" th:utext="${map.user.username}">寒江雪</u> 发布于 <b th:text="${#dates.format(map.post.createTime,'yyyy-MM-dd HH:mm:ss')}">2019-04-15 15:32:18</b>
<ul class="d-inline float-right">
<li class="d-inline ml-2">赞 11</li>
<li class="d-inline ml-2">|</li>
<li class="d-inline ml-2">回帖 <span th:text="${map.post.commentCount}">7</span></li>
</ul>
</div>
</div>
</li>
</ul>
网上搜集解决办法得出这样子错误的可能大概为以下几种:
- 字段拼写错误;
- 前端使用Thymeleaf标签时,报错的字段不是当前控制器方法中返回的Session的字段名;
- 静态页面使用了thymeleaf的表达式接收后台controller传输的对象(数据),但是后台却没有把这个对象传过来,或者传过来一个空对象,所有报错,意思是找不见这个字段(当然对象都没有哪来的字段)。
- 前端没获取到这个字段
经过检查和debug:
- 检查字段名,没有拼写错误,排除;
- 检查bean类,service,dao都有该字段,没问题,排除。
- debug看controller里model确实传入了含该字段的对象,排除
以上可以证明问题并不是出在该字段上,我把th:src="
m
a
p
.
u
s
e
r
.
h
e
a
d
e
r
U
r
l
"
改
为
静
态
值
后
,
就
提
示
我
t
h
:
u
t
e
x
t
=
"
{map.user.headerUrl}"改为静态值后,就提示我th:utext="
map.user.headerUrl"改为静态值后,就提示我th:utext="{map.user.username}"这里出错,所以现在问题很明确了,是map.user有问题,根据网上提示,
在获取map.user的字段时,需要先判断他是否为空,于是把代码修改为
<th:block th:if="${map.user !=null}">
<a href="site/profile.html">
<img th:src="${map.user.headerUrl}" class="mr-4 rounded-circle" alt="用户头像" style="width:50px;height:50px;">
</a>
.....
</th:block>
问题解决。
玄学的是,下午我打算记录记录这个错误时,为了获取早上的错误信息,我把添加的判断删除后,它竟然没有报错,访问一切正常。嗯,让我迷惑了,不知道到底是哪里的问题,或许是看idea的心情吧。