0

I am working on a user login form. It is on my "home.html" ,

I want to show the username after login(when I get session datas form

"login.html" by "Httpsession")

Otherwise just hide when it is empty.

<span th:text="${user.name}" id="uname"></span>

When it is empty, I have an error message like this:

EL1007E: Property or field 'name' cannot be found on null.

Thanks.

  • If you are using Spring, you can use the safe navigation operator - see [here](https://stackoverflow.com/questions/20636456/using-thymeleaf-when-the-value-is-null) for an example. Otherwise, you can use Thymeleaf's `th:if` tag - see [here](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#simple-conditionals-if-and-unless). – andrewJames Jun 04 '20 at 16:06

1 Answers1

2

Use th:if to conditionally include the <span>:

<span th:if="${user?.name}" th:text="${user.name}" id="uname"></span>

To avoid the NullPointerException, use the Safe Navigation Operator: ?..

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211