Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: <label> to have dynamic text concatenated with static text Spring MVC

I'm trying to append some text to a dynamic text as shown below:

<label th:text="Hello ${worldText}"></label>

But the UI throws:

TemplateProcessingException: Could not parse as expression: "Hello ${worldText}

Does anyone know how can I achieve this?

like image 361
Vasudev Avatar asked Sep 08 '17 14:09

Vasudev


People also ask

Is Thymeleaf supported by Spring MVC?

Thymeleaf offers a set of Spring integrations that allow you to use it as a fully-featured substitute for JSP in Spring MVC applications.

How do you display text in Thymeleaf?

In this case, you should use th:text . For example, Username: <p th:text=${account. username}>Username will be rendered here</p> . Note that, the text inside p tag won't be shown.

Is Thymeleaf fully integrated with Spring?

It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates.

What is #{} in Thymeleaf?

Variable expressions are OGNL expressions –or Spring EL if you're integrating Thymeleaf with Spring. *{} is used for selection expressions. Selection expressions are just like variable expressions, except they will be executed on a previously selected object. #{} is used for message (i18n) expressions.


2 Answers

An easy solution would be to insert a span to the label:

<label>Hello <span th:text="${worldText}"></span></label>

But i would prefer to combine text and variables like this:

<label th:text="'Hello' + ${worldText}"></label>
like image 87
Benjamin Schüller Avatar answered Sep 20 '22 11:09

Benjamin Schüller


Another straightforward solution is

<label th:text="${'Hello ' + worldText}"></label>
like image 45
michaeak Avatar answered Sep 19 '22 11:09

michaeak