Furkan Zumrut

Thymeleaf Spring MVC Integration Example

Thymeleaf is an  xml, xhtml, html5 template engine and good alternative  for JSP.   You can see how we integrate it with spring mvc.

package com.springthymeleaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class HelloController {

	@RequestMapping(value = "/{name}", method = RequestMethod.GET)
	public String getMovie(@PathVariable String name, ModelMap model) {
		model.addAttribute("name", name);
		return "hello";
	}

}

 

<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Spring Thymeleaf Example</display-name>

	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<context:component-scan base-package="com.springthymeleaf.controller" />

	<mvc:annotation-driven />


	<bean id="templateResolver"
		class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
		<property name="prefix" value="/WEB-INF/templates/" />
		<property name="suffix" value=".html" />
		<property name="templateMode" value="HTML5" />
	</bean>

	<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
		<property name="templateResolver" ref="templateResolver" />
	</bean>

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

</beans>


 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Spring MVC + Thymeleaf Example</title>
</head>
<body>
	<td>Hello, <span th:text="${name}" />!</td>
</body>
</html>

 

You can download this project from github.

https://github.com/furkanzumrut/SpringThymeleafExample

Just use mvn tomcat:run command in console to see the example how works.

More from my site

  • Getting Started with Scalate: Simple Web Application with Spring MVC
  • Open Source Projects Using Spring Hibernate Technologies
  • Java Restful Web Servisi oluşturmak
  • Using Gzip Compression in Spring Boot
  • [Soap Project] CarSalesWebService Source Code (APACHE CXF)
  • org.hibernate.LazyInitializationException: could not initialize proxy – no Session hatası ve çözümü
spring mvc thymeleaf, thmyleaf spring mvc integration
Spring

Recent Posts

  • How to run java jar in Elastic beanstalk with custom jvm parameters? (Procfile)
  • How to change timezone with ebextension in Elastic Beanstalk?
  • Save your money shutting down EC2 Instance with AWS Lambda!
  • Keep your Test Environment more stable with Pre-Branch
  • Getting Started with Scalate: Simple Web Application with Spring MVC

Medium

Pages

  • CV

Categories

  • Android
  • AWS
  • CXF
  • Devops
  • Genel
  • Java
  • Javascript
  • JAX-RS
  • JPA/Hibernate
  • JSF
  • Maven
  • Postgresql
  • Servlet
  • Spring
  • Tomcat

Archives

  • August 2017
  • June 2016
  • October 2015
  • June 2015
  • May 2015
  • April 2015
  • February 2015
  • December 2014
  • November 2014
  • October 2014
  • June 2014
  • May 2014
  • June 2011

Theme created by Mick de Graaf

age of civilization
tank stars