Introduction

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 Standard Dialects

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:

  • Variable Expressions: ${...}
  • Selection Variable Expressions: *{...}
  • Message Expressions: #{...}
  • Link URL Expressions: @{...}
  • Fragment Expressions: ~{...}

Here are some literals you’ll likely be using:

  • Text literals: 'hello world''Welcome to stackabuse',…
  • Number literals: 012367.90, …
  • Boolean literals: truefalse
  • Null literal: null

Basic Operations:

  • String concatenation: +

  • Literal substitutions: |Welcome to ${city}|

  • Binary operators: +-*/, `%

  • Binary operators: andor

  • Boolean negation (unary operator): !not

Comparisons:

  • Comparators: ><>=<= (gtltgele)
  • Equality operators: ==!= (eqne)

Conditionals:

  • If-then: (if) ? (then)
  • If-then-else: (if) ? (then) : (else)
  • Default: (value) ?: (defaultvalue)

All of these expressions can be used in combination with one another to get the desired results.

#java #spring #spring boot

Getting Started with Thymeleaf in Java and Spring
2.10 GEEK