How to define template for multipage with Apache Tiles in Spring Boot

In the previous post, we have started with Apache Tiles. We know the way to define page fragments (each fragment as a tile) and combine them into a larger page (as a root with many tiles).

This tutorial introduces a way to define template for multipage with Apache Tiles in Spring Boot.

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Overview
1. Project Structure

WebController is a Controller which has request mapping methods to display our full page.
TilesConfiguration contains Beans for ViewResolver and TilesConfigurer.
WEB-INF folder contains all necessary JSP files for tails and a Tails definition XML file.
Our template definition should be:

Dependencies for Spring Boot and Tiles in pom.xml.

2. Step to do

– Create Spring Boot project & add Dependencies
– Create jsp files for tails
– Create definition for tails and JSP layout
– Create Tails Configuration Class
– Create Web Controller
– Run Spring Boot Application & Enjoy Result

III. Practice
1. Create Spring Boot project & add Dependencies

Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields. Remember that we will the Packaging is War.

Click Next, then click Finish.

Open pom.xml and add Dependencies:

2. Create jsp files for tails

Under src/main/webapp folder, create new folder named WEB-INF, then create 3 JSP files in tiles folder:




3. Create definition for tails and JSP layout

In WEB-INF folder, create tail.xml:

definition name="app.default" template="/WEB-INF/layouts/default.jsp"
The code above indicates that definition will be use in Web Controller to point to the template default.jsp.
name="app.default" is an identification, we can use it for a calling from Web Controller or for inheritance.

definition name="app.homepage" extends="app.default"
extends indicates that “app.homepage” definition inherits all attributes of “app.default” including title, header, footer. So we don’t need to rewrite them again.

put-attribute name="header" value="/WEB-INF/tiles/header.jsp"
Each attribute has:
– a name referenced to element in default.jsp.
– a value which are correlative to a JSP file or a specified value.

Then, in /WEB-INF/layouts folder, create a template for gathering all tiles above: default.jsp

4. Create Tails Configuration Class

Under package config, create TilesConfiguration class:

5. Create Web Controller

Under package controller, create WebController class:

Notice that we return “app.homepage” and “app.about” to match with the name of tile-definition defined in tails.xml.

6. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Request 1:

Request 2:

IV. Source Code


By grokonez | February 11, 2017.

Last updated on June 4, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *