2

In thymeleaf 2.1.4, when evaluating null String values with

<p th:inline="text">[[${bean.myNullvar}]]</p> 

The outlined html is

<p>null</p> 

When I use it with th:text it works correctly:

<p th:text="${myNullvar}"></p> 

is outlined as:

<p></p> 

¿Is there any way to render String null values as empty ones with inlining? ¿How is it going to work in Thyeleaf 3, where inline is active by default?

Thanks in advance

5
  • In your first example your variable is within the p-tag; in the second it's not. Commented Apr 27, 2016 at 17:16
  • Both cases are valid uses of thymeleaf, but the first one renders null as "null" instead of empty string. I want to know if there is any way to make inlining work like explicit th:text for null values. Commented Apr 27, 2016 at 18:13
  • Don't know how to put this: Both statements are barely "comparable". Please, learn the difference between the value of an attribute and innerHTML. Commented Apr 27, 2016 at 18:24
  • The question is just related to the way thymeleaf processes templates. Following their documentation, the unique drawback of using inlines is that unprocessed templates are less "friendy". I am aware there are many other features not available (yet) to inlined text, like injecting unescaped text, buit I have not found any mention to this behaviour and I would like to know if it is a bug or a feature, and if it is going to change in future stable 3.x releases. Commented Apr 27, 2016 at 18:47
  • Ok. I don't know, if they are going to change this, but it seems like a "consistent" behaviour. So - for the moment - you'd have to rewrite your (inline-)methods so they always return a String, hence an empty String if the value is null. Commented Apr 27, 2016 at 19:08

1 Answer 1

4

Thymeleaf has some string utility method you can try

Here is the null safe toString.

${#strings.toString(obj)} 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I will try [[${#strings.toString(obj)}]] (verbose but cohernet with how I am creating my templates)
@Basa not a problem. Here is a link to all the string utility methods that Thymeleaf supports. thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#strings
I have tried that expresion, but only checks null for the beans preceding the values, ${#strings.toString(emnployee.name)} == ${employee?.name}. There is however an utiliy that do the job: ${#strings.defaultString(obj,'')}. Thanks again @ndrone.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.