Thymeleaf Integration With Spring

Integrating thymeleaf with Spring framework
Note: This is intended for advanced programmers who already knows the spring,Maven, thymeleaf basics.

Thymeleaf will provide view templates for spring web applications like JSP’s.

1. Thymeleaf dependencies configuration
Add following dependencies in pom.xml

<dependency >
      <groupId> org.thymeleaf</groupId >
      <artifactId> thymeleaf</artifactId >
      <version> ${thymeleaf.version}</version >
      <scope> compile</scope >
      <groupId> org.thymeleaf</groupId >
      <artifactId> thymeleaf-spring3 </artifactId>
      <version> ${thymeleaf.version}</version >
      <scope> compile</scope >

2. Configure following belans spring applicationContext.xml file. The view resolver will handle all the view request.

 <bean id="templateResolver"
    <property name="prefix" value="/WEB-INF/templates/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
    <!-- Template cache is true by default. Set to false if you want -->
    <!-- templates to be automatically updated when modified.        -->
    <property name="cacheable" value="true" />

  <bean id="templateEngine"
    <property name="templateResolver" ref="templateResolver" />

  <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />

3. write the template pages to for displaying in web site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s