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.jspmain tutorial page content ..........
src/main/webapp/WEB-INF/jsp/body/java-tutorial.jspJava tutorials content........
src/main/webapp/WEB-INF/jsp/body/spring-tutorial.jspSpring 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 ProjectDependencies and Technologies Used: - spring-webmvc 5.0.0.RELEASE (Spring Web MVC)
Version Compatibility: 4.1.0.RELEASE - 5.3.39
- 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
|