Property or field 'username' cannot be found on null

报错原因:

username不能为空

解决办法:

原来的代码:

<span th:text="${session.user.username}"></span>

更改后代码:

<span th:text="${session.user?.username}"></span>

为什么加个?就可以解决

因为这是一个thymeleaf判断对象是否为空的方法。thymeleaf中显示某对象使用${username},但如果username为null,thymeleaf就会报错。

猜你喜欢

转载自blog.csdn.net/weixin_41699562/article/details/103208848