When developing web applications, an important choice is which engine will be taking care of the view layer.
Java Server Pages (JSPs) used to be very popular, though the overhead and time-consumption were some major drawbacks to using them. They required a fair bit of change to the HTML of the pages.
Nowadays, Thymeleaf is widely adopted and used as the templating engine for Spring/MVC applications. It can also be used for rich HTML email templating. While JSPs are compiled to Java servlet classes, Thymeleaf parses the plain HTML template files. Based on the expressions present in the file, it generates static content. It’s capable of processing HTML, XML, JS, CSS, etc.
Thymeleaf provides a wide range of attribute processors out of the box as a part of its Standard Dialects. These processors are enough for most typical template processing. Though, you could also extend them to make custom attribute processors if need be.
Let’s take a look at the most important segment of the dialect - the Standard Expression Features. These are some of the expressions you’ll be using fairly regularly:
${...}
*{...}
#{...}
@{...}
~{...}
Here are some literals you’ll likely be using:
'hello world'
, 'Welcome to stackabuse'
,…0
, 123
, 67.90
, …true
, false
null
Basic Operations:
String concatenation: +
Literal substitutions: |Welcome to ${city}|
Binary operators: +
, -
, *
, /
, `%
Binary operators: and
, or
Boolean negation (unary operator): !
, not
Comparisons:
>
, <
, >=
, <=
(gt
, lt
, ge
, le
)==
, !=
(eq
, ne
)Conditionals:
(if) ? (then)
(if) ? (then) : (else)
(value) ?: (defaultvalue)
All of these expressions can be used in combination with one another to get the desired results.
#java #spring #spring boot