In this tutorials, I am going to show you how to work with Spring MVC With Apache Tiles.

Spring MVC Tiles :

Spring MVC Tiles are mostly used combination. Apache Tiles is a templating framework helpful to minimize the development effort of web application (Spring, Struts) user interfaces.

Technologies Used :

  • Spring 4.3.4.RELEASE
  • Apache Tiles 3.0.7
  • Servlet API 3.1.0
  • Maven 3.6.0
  • Java 1.7 and
  • STS 3.6.4.RELEASE

Spring MVC Tiles Example :

Project Structure :

Spring MVC Tiles Project

Project Dependencies :

pom.xml


<properties>

        <springframework.version>4.3.4.RELEASE</springframework.version>
        <apache-tiles.version>3.0.7</apache-tiles.version>
        <javax.servlet-api.version>3.1.0</javax.servlet-api.version>
        <javax.servlet.jsp-api.version>2.3.1</javax.servlet.jsp-api.version>
        <jstl.version>1.2</jstl.version>
        <java.version>1.7</java.version>

        <maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
        <maven-war-plugin.version>2.6</maven-war-plugin.version>

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- Apache Tiles -->
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>${apache-tiles.version}</version>
        </dependency>
        <!-- Servlet+JSP+JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet-api.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>${javax.servlet.jsp-api.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
    </dependencies>

Create ApplicationConfiguration file, since we are using Servlet 3.x version, there is no web.xml. The below class replaces the web.xml role.

ApplicationConfig.java


package com.onlinetutorialspoint.tiles.config;

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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.onlinetutorialspoint.tiles")
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/views/**/tiles.xml" });
        tilesConfigurer.setCheckRefresh(true);
        return tilesConfigurer;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        TilesViewResolver viewResolver = new TilesViewResolver();
        registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

}

Create ApplicationInitializer to start up the application.

ApplicationInitializer.java


package com.onlinetutorialspoint.tiles;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.onlinetutorialspoint.tiles.config.ApplicationConfig;

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { ApplicationConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

Create a Controller.

HomeController.java


package com.onlinetutorialspoint.tiles.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class HomeController {
    @RequestMapping(value = { "/" }, method = RequestMethod.GET)
    public String homePage(ModelMap model) {
        return "home";
    }

    @RequestMapping(value = { "/admin" }, method = RequestMethod.GET)
    public String productsPage(ModelMap model) {
        return "admin";
    }

    @RequestMapping(value = { "/user" }, method = RequestMethod.GET)
    public String contactUsPage(ModelMap model) {
        return "user";
    }
}

Create necessary views:

home.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
    <h2>Welcome to OnlineTutorialsPoint Spring MVC Tiles Tutorials</h2>
</body>
</html>

user.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC</title>
</head>
<body>
    <h2>Welcome User :)</h2>
</body>
</html>

admin.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Apache Tiles</title>
</head>
<body>
    <h2>Welcome Admin :)</h2>
</body>
</html>

Configure the Apache tiles:

tiles.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!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>  
  
   <!-- Template Definition -->
   <definition name="template-def"
       template="/WEB-INF/views/tiles/layouts/defaultLayout.jsp">  
       <put-attribute name="title" value="" />  
       <put-attribute name="header" value="/WEB-INF/views/tiles/templates/header.jsp" />  
       <put-attribute name="menu" value="/WEB-INF/views/tiles/templates/menu.jsp" />  
       <put-attribute name="body" value="" />  
       <put-attribute name="footer" value="/WEB-INF/views/tiles/templates/footer.jsp" />  
   </definition>  
  
   <!-- Main Page -->
   <definition name="home" extends="template-def">  
       <put-attribute name="title" value="Welcome" />  
       <put-attribute name="body" value="/WEB-INF/views/pages/home.jsp" />  
   </definition>  
 
   <!-- User Page -->
   <definition name="user" extends="template-def">  
       <put-attribute name="title" value="User" />  
       <put-attribute name="body" value="/WEB-INF/views/pages/user.jsp" />  
   </definition>  
       
   <!-- Admin Page -->
   <definition name="admin" extends="template-def">  
       <put-attribute name="title" value="Admin" />  
       <put-attribute name="body" value="/WEB-INF/views/pages/admin.jsp" />  
   </definition>  
  
</tiles-definitions>

header.jsp

<header>
  <h1>Welcome To OnlineTutorialsPoint MVC Ttorials</h1>
</header>

footer.jsp


<footer>copyright © OnlineTutorialsPoint</footer>

menu.jsp


<nav class="nav">
    <a href="${pageContext.request.contextPath}/"></a>
    <ul id="menu">
       <li><a href="${pageContext.request.contextPath}/">Home</a></li>
       <li><a href="${pageContext.request.contextPath}/user">User</a></li>
       <li><a href="${pageContext.request.contextPath}/admin">Admin</a></li>
    </ul>
</nav>

Run the application :

http://localhost:8080/spring-mvc-tiles/

Home Screen :

Spring MVC Tiles 1

User Screen :

Spring MVC Tiles 2

Admin Screen :

Spring MVC Tiles 3

Happy Learning 🙂