Using Apache Tiles in Spring MVC

[Updated: Sep 20, 2017, Created: Sep 7, 2017]

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

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

@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig extends WebMvcConfigurerAdapter {

  @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;
  }

 /* @Bean
  public ViewResolver tilesViewResolver() {
      return new TilesViewResolver();
  }
}
  • TilesConfigurer

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

  • WebMvcConfigurerAdapter#configureViewResolvers

    The method ViewResolverRegistry#titles() registers an instance of TilesViewResolver as bean. Alternatively, we can register the bean ourselves:
    @Bean
    public ViewResolver tilesViewResolver() {
        return new TilesViewResolver();
    }
    


Writing a Spring Controller

@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........

Output

Example Project

Dependencies and Technologies Used :

  • spring-webmvc 4.3.10.RELEASE: Spring Web MVC.
  • 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.1.0 Java Servlet API
  • jstl 1.2 javax.servlet:jstl
  • JDK 1.8
  • Maven 3.3.9

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

See Also