Spring MVC and Thymeleaf Integration

Thymeleaf is an alternative as replacement of JSP, please visit http://www.thymeleaf.org/ for the further information about thymeleaf. Now I will show you how to integrate Spring MVC and Thymeleaf.

Maven Dependency :


and the Java Configuration :

@ComponentScan(basePackages = { "com.hidupbersahaja.controller" })
public class WebappConfig extends WebMvcConfigurerAdapter{
	private org.apache.commons.configuration.Configuration _config = ConfigManager.getConfiguration();
	 * Servlet Context Template Resolver
	 * @return
	public ServletContextTemplateResolver getServletContextTemplateResolver(){
		ServletContextTemplateResolver scTemplateResolver = new ServletContextTemplateResolver();
		return scTemplateResolver;
	 * Spring Template Engine 
	 * @return
	public SpringTemplateEngine getTemplateEngine(){
		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		return templateEngine;
	 * Thymeleaf View Resolver
	 * @return
	public ThymeleafViewResolver getThymeleafViewResolver(){
		ThymeleafViewResolver resolver = new ThymeleafViewResolver();
		return resolver;
	 * Content Negotiate 
	 * to allowing request and respond content type 
	 * such as html, xml, json
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
                .mediaType("json", MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML)

and the Controller side :

public class FAQController {

	 * default handler
	 * @return faq.html
	@RequestMapping(method = RequestMethod.GET)
	public String defaultHandler(Model model) {
		return "faq";

Create faq.html inside /WEB-INF/templates/ directory

<!doctype html>
<html lang="en">


	<h2>I am using Thymeleaf, how about you ? </h2>


That’s all … Good Luck

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>