Close

Using Apache Tiles in Spring MVC (legacy)

[Last Updated: Oct 8, 2026]

This example demonstrates how to use Apache Tiles as the view technology in a Spring MVC application.

Important: Tiles support was deprecated in the Spring Framework 5.x line and removed in Spring Framework 6.0 (the org.springframework.web.servlet.view.tiles3 package no longer exists). This example works only with Spring Framework 5.3.x or earlier, and is kept here for legacy projects and for version-history purposes. In Spring Framework 6.x and 7.x, TilesConfigurer and TilesViewResolver are not available. For new applications, consider a template engine with layout or fragment support, such as Thymeleaf (layouts via fragments or the Thymeleaf Layout Dialect), FreeMarker (template inheritance via macros/includes), or SiteMesh 3 for decorating pages.

Example

Additional Maven dependencies

pom.xml

<dependency>
   <groupId>org.apache.tiles</groupId>
   <artifactId>tiles-extras</artifactId>
   <version>3.0.7</version>
</dependency>

Java Config class

package com.logicbig.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;

@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.tiles();
    }

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions("/WEB-INF/tiles-def.xml");
        return tilesConfigurer;
    }
}
  • TilesConfigurer

    A helper class to configure Tiles 3.x for the Spring Framework. The method TilesConfigurer#setDefinitions() sets the list of files containing Tiles definitions.

  • WebMvcConfigurer#configureViewResolvers

    The method ViewResolverRegistry#tiles() registers an instance of TilesViewResolver as a bean.

Writing a Spring Controller

package com.logicbig.example;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.time.LocalTime;

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

    @GetMapping("/")
    public String mainTutorial(Model model) {
        addCommonAttributes(model);
        return "defMainTutorial";
    }

    @GetMapping("/java")
    public String javaTutorial(Model model) {
        addCommonAttributes(model);
        return "defJavaTutorial";
    }

    @GetMapping("/spring")
    public String springTutorial(Model model) {
        addCommonAttributes(model);
        return "defSpringTutorial";
    }

    private void addCommonAttributes(Model model) {
        model.addAttribute("time", LocalTime.now());
    }
}

The Tiles definition file

src/main/webapp/WEB-INF/tiles-def.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>

    <definition name="defCommon" template="/WEB-INF/jsp/layouts/main-layout.jsp">
        <put-attribute name="title" value=""/>
        <put-attribute name="header" value="/WEB-INF/jsp/main-header.jsp"/>
        <put-attribute name="menu" value="/WEB-INF/jsp/main-menu.jsp"/>
        <put-attribute name="body" value=""/>
        <put-attribute name="footer" value="/WEB-INF/jsp/main-footer.jsp"/>
    </definition>

    <definition name="defMainTutorial" extends="defCommon">
        <put-attribute name="title" value="Tutorials"/>
        <put-attribute name="body" value="/WEB-INF/jsp/body/tutorial.jsp"/>
    </definition>

    <definition name="defJavaTutorial" extends="defCommon">
        <put-attribute name="title" value="Java Tutorials"/>
        <put-attribute name="body" value="/WEB-INF/jsp/body/java-tutorial.jsp"/>
    </definition>

    <definition name="defSpringTutorial" extends="defCommon">
        <put-attribute name="title" value="Spring Tutorials"/>
        <put-attribute name="body" value="/WEB-INF/jsp/body/spring-tutorial.jsp"/>
    </definition>
</tiles-definitions>

The JSP files

src/main/webapp/WEB-INF/jsp/layouts/main-layout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
    <title><tiles:getAsString name="title"/></title>
</head>
<body>
<table style="width:100%;height:100%">
    <tr>
        <td colspan="2">
            <tiles:insertAttribute name="header" />
        </td>
    </tr>
    <tr>
        <td>
            <tiles:insertAttribute name="menu" />
        </td>
        <td style="height:100%;">
            <tiles:insertAttribute name="body" />
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <tiles:insertAttribute name="footer" />
        </td>
    </tr>
</table>
</body>
</html>

src/main/webapp/WEB-INF/jsp/main-header.jsp

<div style="text-align:center;background: #ddd; border-bottom:solid 1px #00f">
The header ....
<br/>
Time: ${time}
</div>

src/main/webapp/WEB-INF/jsp/main-menu.jsp

<div style="width: 0px ;min-width: 200px;  background:pink; height:100%;">
<ul style="list-style:none;">
<li><a href="/">Tutorials Main Page</a></li>
<li><a href="/java">Java Tutorials</a></li>
<li><a href="/spring">Spring Tutorials</a></li>
<li>more stuff...</li>
</ul>
</div>

src/main/webapp/WEB-INF/jsp/main-footer.jsp

<div style="background: yellow; text-align:center; min-width: 100%; height: 40px;">
    The footer...
</div>

src/main/webapp/WEB-INF/jsp/body/tutorial.jsp

main tutorial page content ..........

src/main/webapp/WEB-INF/jsp/body/java-tutorial.jsp

Java tutorials content........

src/main/webapp/WEB-INF/jsp/body/spring-tutorial.jsp

Spring tutorials content........

Running the example

To try examples, run embedded Jetty (configured in pom.xml of example project below):

mvn jetty:run

Output

$ curl -s "http://localhost:8080"

<html>
<head>
<title>Tutorials</title>
</head>
<body>
<table style="width:100%;height:100%">
<tr>
<td colspan="2">
<div style="text-align:center;background: #ddd; border-bottom:solid 1px #00f">
The header ....
<br/>
Time: 20:50:22.046
</div>
</td>
</tr>
<tr>
<td>
<div style="width: 0px ;min-width: 200px; background:pink; height:100%;">
<ul style="list-style:none;">
<li><a href="/">Tutorials Main Page</a></li>
<li><a href="/java">Java Tutorials</a></li>
<li><a href="/spring">Spring Tutorials</a></li>
<li>more stuff...</li>
</ul>
</div>
</td>
<td style="height:100%;">
main tutorial page content ..........
</td>
</tr>
<tr>
<td colspan="2">
<div style="background: yellow; text-align:center; min-width: 100%; height: 40px;">
The footer...
</div>
</td>
</tr>
</table>
</body>
</html>
$ curl -s "http://localhost:8080/java"

<html>
<head>
<title>Java Tutorials</title>
</head>
<body>
<table style="width:100%;height:100%">
<tr>
<td colspan="2">
<div style="text-align:center;background: #ddd; border-bottom:solid 1px #00f">
The header ....
<br/>
Time: 20:50:52.571
</div>
</td>
</tr>
<tr>
<td>
<div style="width: 0px ;min-width: 200px; background:pink; height:100%;">
<ul style="list-style:none;">
<li><a href="/">Tutorials Main Page</a></li>
<li><a href="/java">Java Tutorials</a></li>
<li><a href="/spring">Spring Tutorials</a></li>
<li>more stuff...</li>
</ul>
</div>
</td>
<td style="height:100%;">
Java tutorials content........
</td>
</tr>
<tr>
<td colspan="2">
<div style="background: yellow; text-align:center; min-width: 100%; height: 40px;">
The footer...
</div>
</td>
</tr>
</table>
</body>
</html>
$ curl -s "http://localhost:8080/spring"

<html>
<head>
<title>Spring Tutorials</title>
</head>
<body>
<table style="width:100%;height:100%">
<tr>
<td colspan="2">
<div style="text-align:center;background: #ddd; border-bottom:solid 1px #00f">
The header ....
<br/>
Time: 20:51:20.229
</div>
</td>
</tr>
<tr>
<td>
<div style="width: 0px ;min-width: 200px; background:pink; height:100%;">
<ul style="list-style:none;">
<li><a href="/">Tutorials Main Page</a></li>
<li><a href="/java">Java Tutorials</a></li>
<li><a href="/spring">Spring Tutorials</a></li>
<li>more stuff...</li>
</ul>
</div>
</td>
<td style="height:100%;">
Spring tutorials content........
</td>
</tr>
<tr>
<td colspan="2">
<div style="background: yellow; text-align:center; min-width: 100%; height: 40px;">
The footer...
</div>
</td>
</tr>
</table>
</body>
</html>

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 5.0.0.RELEASE (Spring Web MVC)
     Version Compatibility: 4.1.0.RELEASE - 5.3.39Version List
    ×

    Version compatibilities of spring-webmvc with this example:

      javax.servlet-api:3.x
    • 4.1.0.RELEASE
    • 4.1.1.RELEASE
    • 4.1.2.RELEASE
    • 4.1.3.RELEASE
    • 4.1.4.RELEASE
    • 4.1.5.RELEASE
    • 4.1.6.RELEASE
    • 4.1.7.RELEASE
    • 4.1.8.RELEASE
    • 4.1.9.RELEASE
    • 4.2.0.RELEASE
    • 4.2.1.RELEASE
    • 4.2.2.RELEASE
    • 4.2.3.RELEASE
    • 4.2.4.RELEASE
    • 4.2.5.RELEASE
    • 4.2.6.RELEASE
    • 4.2.7.RELEASE
    • 4.2.8.RELEASE
    • 4.2.9.RELEASE
    • 4.3.0.RELEASE
    • 4.3.1.RELEASE
    • 4.3.2.RELEASE
    • 4.3.3.RELEASE
    • 4.3.4.RELEASE
    • 4.3.5.RELEASE
    • 4.3.6.RELEASE
    • 4.3.7.RELEASE
    • 4.3.8.RELEASE
    • 4.3.9.RELEASE
    • 4.3.10.RELEASE
    • 4.3.11.RELEASE
    • 4.3.12.RELEASE
    • 4.3.13.RELEASE
    • 4.3.14.RELEASE
    • 4.3.15.RELEASE
    • 4.3.16.RELEASE
    • 4.3.17.RELEASE
    • 4.3.18.RELEASE
    • 4.3.19.RELEASE
    • 4.3.20.RELEASE
    • 4.3.21.RELEASE
    • 4.3.22.RELEASE
    • 4.3.23.RELEASE
    • 4.3.24.RELEASE
    • 4.3.25.RELEASE
    • 4.3.26.RELEASE
    • 4.3.27.RELEASE
    • 4.3.28.RELEASE
    • 4.3.29.RELEASE
    • 4.3.30.RELEASE
    • 5.0.0.RELEASE
    • 5.0.1.RELEASE
    • 5.0.2.RELEASE
    • 5.0.3.RELEASE
    • 5.0.4.RELEASE
    • 5.0.5.RELEASE
    • 5.0.6.RELEASE
    • 5.0.7.RELEASE
    • 5.0.8.RELEASE
    • 5.0.9.RELEASE
    • 5.0.10.RELEASE
    • 5.0.11.RELEASE
    • 5.0.12.RELEASE
    • 5.0.13.RELEASE
    • 5.0.14.RELEASE
    • 5.0.15.RELEASE
    • 5.0.16.RELEASE
    • 5.0.17.RELEASE
    • 5.0.18.RELEASE
    • 5.0.19.RELEASE
    • 5.0.20.RELEASE
    • 5.1.0.RELEASE
    • 5.1.1.RELEASE
    • 5.1.2.RELEASE
    • 5.1.3.RELEASE
    • 5.1.4.RELEASE
    • 5.1.5.RELEASE
    • 5.1.6.RELEASE
    • 5.1.7.RELEASE
    • 5.1.8.RELEASE
    • 5.1.9.RELEASE
    • 5.1.10.RELEASE
    • 5.1.11.RELEASE
    • 5.1.12.RELEASE
    • 5.1.13.RELEASE
    • 5.1.14.RELEASE
    • 5.1.15.RELEASE
    • 5.1.16.RELEASE
    • 5.1.17.RELEASE
    • 5.1.18.RELEASE
    • 5.1.19.RELEASE
    • 5.1.20.RELEASE
    • 5.2.0.RELEASE
    • 5.2.1.RELEASE
    • 5.2.2.RELEASE
    • 5.2.3.RELEASE
    • 5.2.4.RELEASE
    • 5.2.5.RELEASE
    • 5.2.6.RELEASE
    • 5.2.7.RELEASE
    • 5.2.8.RELEASE
    • 5.2.9.RELEASE
    • 5.2.10.RELEASE
    • 5.2.11.RELEASE
    • 5.2.12.RELEASE
    • 5.2.13.RELEASE
    • 5.2.14.RELEASE
    • 5.2.15.RELEASE
    • 5.2.16.RELEASE
    • 5.2.17.RELEASE
    • 5.2.18.RELEASE
    • 5.2.19.RELEASE
    • 5.2.20.RELEASE
    • 5.2.21.RELEASE
    • 5.2.22.RELEASE
    • 5.2.23.RELEASE
    • 5.2.24.RELEASE
    • 5.2.25.RELEASE
    • 5.3.0
    • 5.3.1
    • 5.3.2
    • 5.3.3
    • 5.3.4
    • javax.servlet-api:4.x
    • 5.3.5
    • 5.3.6
    • 5.3.7
    • 5.3.8
    • 5.3.9
    • 5.3.10
    • 5.3.11
    • 5.3.12
    • 5.3.13
    • 5.3.14
    • 5.3.15
    • 5.3.16
    • 5.3.17
    • 5.3.18
    • 5.3.19
    • 5.3.20
    • 5.3.21
    • 5.3.22
    • 5.3.23
    • 5.3.24
    • 5.3.25
    • 5.3.26
    • 5.3.27
    • 5.3.28
    • 5.3.29
    • 5.3.30
    • 5.3.31
    • 5.3.32
    • 5.3.33
    • 5.3.34
    • 5.3.35
    • 5.3.36
    • 5.3.37
    • 5.3.38
    • 5.3.39

    Versions in green have been tested.

  • tiles-extras 3.0.7 (Extras for Tiles, including non-standard, non-generic, non-identifiable components that may be useful for Tiles users)
  • javax.servlet-api 3.0.1 (Java Servlet API)
  • jstl 1.2 (javax.servlet:jstl)
  • JDK 1.8
  • Maven 3.9.11

Apache Tiles View in Spring MVC Select All Download
  • spring-with-apache-tiles-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp
          • WEB-INF
            • jsp
              • body
              • layouts
            • tiles-def.xml

    See Also

    Join