Integrate Apache Tiles3 to Spring Boot Web MVC Application

Spring Boot Web MVC configured to produce an executable WAR and ready for fully fledged web application development. In this tutorial, we will see how to use Apache Tiles3 framework in your Spring Boot Web MVC application.

Let us start by creating an application based on spring-boot-starter-web. You may use spring initializer to create a basic project template.

1. Spring Boot Dependencies

Add dependencies to the POM to pull in Tomcat, Jasper and Apache Tiles


If you’re using Gradle build system, you can add the following dependency to your build.gradle file.

compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.12'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
compile group: 'org.apache.tiles', name: 'tiles-jsp', version: '3.0.8'

Change the packaging to WAR in the POM


Or for gradle

apply plugin: 'war'

2. Project Directory Structure

Create Web application structure the Maven way (tree showing all files):

    │   pom.xml
    │   ├───main
    │   │   ├───java
    │   │   │   └───com
    │   │   │       └───example
    │   │   │           └───app
    │   │   │               │
    │   │   │               │
    │   │   │               └───controller
    │   │   │             
    │   │   ├───resources
    │   │   │
    │   │   └───webapp
    │   │       ├───static       
    │   │       └───WEB-INF
    │   │           └───tiles
    │   │               │   tiles.xml
    │   │               ├───layouts
    │   │               │       basic.jsp
    │   │               └───views
    │   │                   │   footer.jsp
    │   │                   │   header.jsp
    │   │                   └───home
    │   │                          greeting.jsp
    │   │                          home.jsp
    │   └───test

3. Tiles Configuration for Spring Boot

Configure tiles by adding a new configuration class – it will be pulled in by classpath scanning:


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

public class TilesConfig {

    public TilesConfigurer tilesConfigurer() {
        final TilesConfigurer configurer = new TilesConfigurer();
        configurer.setDefinitions(new String[]{"WEB-INF/tiles/tiles.xml"});
        return configurer;

    public TilesViewResolver tilesViewResolver() {
        final TilesViewResolver resolver = new TilesViewResolver();
        return resolver;

4. Tiles Layout and Views Declaration

Create the tiles configuration file: WEB-INF/tiles/tiles.xml

<!--?xml version="1.0" encoding="ISO-8859-1" ?-->


    <!-- Templates -->      
    <definition name="layout.basic" template="/WEB-INF/tiles/layouts/basic.jsp">
        <put-attribute name="title" value="Spring Web MVC with Tiles 3">
        <put-attribute name="header" value="/WEB-INF/tiles/views/header.jsp">
        <put-attribute name="body" value="">
        <put-attribute name="footer" value="/WEB-INF/tiles/views/footer.jsp">
    <!-- Pages -->        
    <definition name="site.home" extends="layout.basic">
        <put-attribute name="body" value="/WEB-INF/tiles/views/home/home.jsp">
    <definition name="site.greeting" extends="layout.basic">
        <put-attribute name="body" value="/WEB-INF/tiles/views/home/greeting.jsp">

Create the tiles themselves


<@ taglib uri="" prefix="tiles">
        <title><tiles:getAsString name="title"/></title>
        <tiles:insertattribute name="header"></tiles:insertattribute>
        <tiles:insertattribute name="body"></tiles:insertattribute>
        <tiles:insertattribute name="footer">


<div class="header">Page Header</div>


<div class="footer">Page Footer</div>


<div class="main-contnet">
     Main page content goes here...


  Hello ${name}

5. Spring Boot Controller

Add a controller class into a controller directory:


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

public class HelloWorldController {    

    @RequestMapping(value = "/home", method=RequestMethod.GET)
    public String home() {
        return "site.home";
    @RequestMapping(value = "/greet/{name}", method=RequestMethod.GET)
    public ModelAndView greetTwoWays(@PathVariable(value="name") final String name, final Model model) {        
        return new ModelAndView("site.greeting", "name", name);

Build the project using Maven:

mvn clean package

When using Gradle:

gradle build

Now let us test the controller and confirm that Spring MVC has been configured as expected

nilanchala avtar

Nilanchala Panigrahy

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (