Creating an Expression Object Using Thymeleaf
In this article, see how to create Thymeleaf functions like #string, #dates, #lists.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
In this tutorial, we will learn how to create an Expression object (similar to #string, #dates, #lists), using Thymeleaf. It will check whether a number is even or odd. For this, let's use a quick and practical example.
What Is a Thymeleaf Dialect?
A dialect allows you to add custom functionality to thymeleaf. Thus extending your ability to build and reuse templates.
Maven Dependencies
First, let's add our Thymeleaf dependency to the pom.xml
file:
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
This dependency will allow us to use the standard thymeleaf dialect.
Creating Our Dialect
The creation of our dialect goes through the creation of 3 classes: Utils, Factory and Dialect.
Number Utils Class
The Utils class will contain all of our business logic. It is in this class where all of our Java code will stay.
xxxxxxxxxx
public final class NumberUtils {
public String isEven(Integer number) {
if (number % 2 == 0) {
return number +" is even";
} else {
return number + " is not even";
}
}
}
Factory Class
This class is responsible for generating our utils object and will return it to the thymeleaf template engine.
x
public class NumberExpressionFactory implements IExpressionObjectFactory {
private static final String TEMPORAL_EVALUATION_VARIABLE_NAME = "number";
private static final Set<String> ALL_EXPRESSION_OBJECT_NAMES = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList(TEMPORAL_EVALUATION_VARIABLE_NAME)));
public Set<String> getAllExpressionObjectNames() {
return ALL_EXPRESSION_OBJECT_NAMES;
}
public Object buildObject(
IExpressionContext context, String expressionObjectName) {
if (
TEMPORAL_EVALUATION_VARIABLE_NAME.equals(expressionObjectName)) {
return new NumberUtils();
}
return null;
}
public boolean isCacheable(String expressionObjectName) {
return true;
}
}
In the EVALUATION_VARIABLE_NAME
variable we define the name of the dialect that will be used to generate an object from our NumberUtils class. In the buildObject
method we make a comparison to find out if the dialect to be executed corresponds to that of our factory.
Dialect Class
This class implements an AbstractDialect
and IExpressionObjectDialect
and will be the definition of our dialect.
The first represents an abstract dialect and the second an expression object. With that, thymeleaf will recognize our class as a new dialect that can be used in the processing of templetes. Like the representation of a new expression object.
We created an object from our factory and in the builder, we passed the name of our dialect to thymeleaf. This is the name that will be provided in the buildObject
method of the factory class.
xxxxxxxxxx
public class NumberExpressionDialect extends AbstractDialect implements IExpressionObjectDialect {
private final IExpressionObjectFactory NUMBER_EXPRESSION_OBJECTS_FACTORY = new NumberExpresseionFactory();
public NumberExpressionDialect() {
super("number");
}
public IExpressionObjectFactory getExpressionObjectFactory() {
return PAPEL_EXPRESSION_OBJECTS_FACTORY;
}
}
In getExpressionObjectFactory()
we return an object of type IExpressionObjectFactory
(Just the type of our NumberExpressionFactory
).
Creating MvcConfig
In this file, we will configure all the dialects used by the thymeleaf template engine.
xxxxxxxxxx
public class MvcConfig implements WebMvcConfigurer {
"Thymeleaf template resolver serving HTML 5") (
public ClassLoaderTemplateResolver templateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setCacheable(false);
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
"Thymeleaf template engine with Spring integration") (
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new NumberExpressionDialect());
return templateEngine;
}
}
First, we define our template to resolve and in the next method we add our dialect to the templateEngine of thymeleaf.
The HTML
Just use our dialect and expression like the others
xxxxxxxxxx
<div th:text=”${#number.isEven(10)}”></div>
You can find the complete source code for this article on this GitHub repository, and please feel free to provide your valuable feedback in the comments section.
Opinions expressed by DZone contributors are their own.
Comments