Here’s a quick way to get you started with Thymeleaf 1.0.0. You will need Eclipse Indigo and the m2e Maven Plugin for Eclipse. You will also need a basic understanding of Spring 3 MVC, as we will convert the Spring MVC view from JSPs to Thymeleaf.
1. Create a new Maven Project using the Eclipse New Wizard.
2. When prompted for the archetype, pick the spring-mvc-jpa-archetype. This will give us a running Spring MVC web application.
3. Enter com.teamextension for the groupId. Enter quickthymeleaf for the artifactId. Enter com.teamextension.quickthymeleaf for the package. Hit Finish.
So far we have created a basic Spring MVC web application. Next step is to add Thymeleaf support.
4. Right click on the quickthymeleaf project and go to Maven -> Add Dependency. Enter the pattern thymeleaf and pick the org.thymeleaf thymeleaf-spring3.
This adds the Thymeleaf library to our Spring MVC application.
5. Go to src/main/webapp/WEB-INF/spring/app/servlet-context.xml and make the following changes.
Remove the org.springframework.web.servlet.view.InternalResourceViewResolver bean and replace it with
<beans:bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".html" /> <beans:property name="templateMode" value="HTML5" /> </beans:bean> <beans:bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> <beans:property name="templateResolver" ref="templateResolver" /> </beans:bean> <beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver"> <beans:property name="templateEngine" ref="templateEngine" /> </beans:bean>
This changes the ViewResolver from using JSPs to using Thymeleaf.
6. Now we change the JSPs. Go to src/main/webapp//WEB-INF/views. Rename home.jsp to home.html. Replace the 2 JSP directives on the top of home.html.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %>
with the HTML5 doctype
<!DOCTYPE html>
Also replace
${controllerMessage}
with
<p th:text="${controllerMessage}"></p>
Remember, Thymeleaf is not a sequential text processor and instead works of XML based formats. The final home.html should look like this.
<!DOCTYPE html> <html> <head> <title>Home</title> </head> <body> <h1> Hello world! </h1> <p th:text="${controllerMessage}"></p> </body> </html>
7. Edit src/main/resources/log4j.xml. Look for “org.application” and change it to “com.teamextension”. This will enable debug logging for our web application.
8. Run mvn tomcat:run to test your web application. Here we will create an Eclipse run configuration for Maven so we can run tomcat:run from within Eclipse.
9. Go to http://localhost:8080/quickthymeleaf to see your web application in action.
10. For development purposes, you might want to disable caching. Add the cacheable property to the templateResolver bean to control template caching.
<beans:property name=”cacheable” value=”false” />
Now you have a fully configured Spring 3 MVC + Thymeleaf web application. For a more in-depth look at Thymeleaf, please read the official Thymeleaf Documentation.
I do not have the spring-mvc-jpa-archetype as an option. Do you know where I can download it?
I’m also getting this error:
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: “{$controllerMessage}” (home:10)
Do I need to declare the “th” namespace anywhere?
Hi Eric,
I wasn’t able to encounter your issue.
Here’s my sample app
https://www.box.com/s/p2qb19bjv6nv1z06fvsy