Property or field 'username' cannot be found on null

Reason for error:

username cannot be empty

Solution:

The original code:

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

Code after change:

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

Why add one? Can be solved

Because this is a thymeleaf method to determine whether the object is empty. thymeleaf shows that an object uses ${username}, but if the username is null, thymeleaf will report an error.

Guess you like

Origin blog.csdn.net/weixin_41699562/article/details/103208848