StackTips

Integrate Apache Tiles3 to Spring Boot Web MVC Application

nilan avtar

Written by

Nilanchala,  8 min read,  4.36K views, updated on Sept. 17, 2023

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

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-tomcat</artifactid>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupid>org.apache.tomcat.embed</groupid>
    <artifactid>tomcat-embed-jasper</artifactid>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupid>org.apache.tiles</groupid>
    <artifactid>tiles-jsp</artifactid>
    <version>3.0.4</version>
</dependency>

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

<packaging>war</packaging> 

Or for gradle

    
apply plugin: 'war'

2. Project Directory Structure

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

ExampleApp:
    │   pom.xml
    ├───src
    │   ├───main
    │   │   ├───java
    │   │   │   └───com
    │   │   │       └───example
    │   │   │           └───app
    │   │   │               │   ExampleApplication.java
    │   │   │               │   TilesConfig.java
    │   │   │               └───controller
    │   │   │                       HelloWorldController.java
    │   │   ├───resources
    │   │   │       application.properties
    │   │   └───webapp
    │   │       ├───static       
    │   │       └───WEB-INF
    │   │           └───tiles
    │   │               │   tiles.xml
    │   │               ├───layouts
    │   │               │       basic.jsp
    │   │               └───views
    │   │                   │   footer.jsp
    │   │                   │   header.jsp
    │   │                   └───home
    │   │                          greeting.jsp
    │   │                          home.jsp
    │   └───test
    │       
    └───target

3. Tiles Configuration for Spring Boot

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

package com.example.app;

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;

@Configuration
public class TilesConfig {

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

    @Bean
    public TilesViewResolver tilesViewResolver() {
        final TilesViewResolver resolver = new TilesViewResolver();
        resolver.setViewClass(TilesView.class);
        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" ?-->

<tiles-definitions>

    <!-- 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">
    </put-attribute></put-attribute></put-attribute></put-attribute></definition>
    
    <!-- Pages -->        
    <definition name="site.home" extends="layout.basic">
        <put-attribute name="body" value="/WEB-INF/tiles/views/home/home.jsp">
    </put-attribute></definition>
    
    <definition name="site.greeting" extends="layout.basic">
        <put-attribute name="body" value="/WEB-INF/tiles/views/home/greeting.jsp">
    </put-attribute></definition>
</tiles-definitions>

Create the tiles themselves

basic.jsp:

<@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles">
        <title><tiles:getAsString name="title"/></title>
        <tiles:insertattribute name="header"></tiles:insertattribute>
        <tiles:insertattribute name="body"></tiles:insertattribute>
        <tiles:insertattribute name="footer">
</tiles:insertattribute>

header.jsp:

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

footer.jsp:

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

home.jsp:

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

greeting.jsp:

<div>
  Hello ${name}
</div>

5. Spring Boot Controller

Add a controller class into a controller directory:

package com.example.app.controller;

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;

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

http://localhost:8080/home    
http://localhost:8080/greet/John

Getting Started with Spring Boot- Beginner's Guide

This course covers the fundamentals of Spring Boot an you will gain the necessary skills to develop various types of applications using the powerful features of the Spring Boot framework.

>> CHECK OUT THE COURSE
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.

Related posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!