P
P
P_Alexander2020-05-19 22:34:19
Spring
P_Alexander, 2020-05-19 22:34:19

Themeleaf: How to display a default message on a page?

Good evening!
Such a question, when the model is displayed on the html page, and there is no data, I would like what would be written in the tag - "no data" can this be done on the page itself without resorting to adding an attribute in the controller?
Example:

<span th:text="'Your city : ' + ${address.city}">Non data</span>

I was hoping that if address.city == null then Non data, if not, then the data from the model, but it's not...
Another question, how is this usually done?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-05-20
@P_Alexander

Hello!
Why don't you use the capabilities of the template engine?
Here are a few options for solving the issue:
1) Using the ternary operator

<span th:text="${address.city != null} ? ${address.city} : 'No data!'">City</span>

2) Usage: if unless (similar to if else in java). Note that if city is a string, then you can use ==, and if it's an object, then you need to useeq
<span th:if="${address.city} == null">Non data</span>
<span th:unless="${address.city} != null" th:text="'Your city : ' + ${address.city}">Non data</span>

Also note that you can "secure" the application if the value is null using the secure navigation operator ?.between address & city
<span th:text="'Your city : ' + ${address?.city}">Non data</span>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question