How to start with Apache Tiles in Spring Boot

Apache Tiles allows us to define page fragments (each fragment as a tile) which can be combined into a larger page (as a root with many tiles). They help to reduce duplication of common content, so that we can make a series of reusable templates more easier.

This tutorial shows you how to start 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.
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.homepage" template="/WEB-INF/layouts/default.jsp"
The code above indicates that name="app.homepage" will be use in Web Controller to point to the template default.jsp.

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” 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: Open your browser, go to http://localhost:8080/

IV. Source Code


Related Posts

Got Something To Say:

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