Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects

Monday, 11 March 2013

Spring Tiles Integration Hello World Tutorial

In this blog we will talk about What is Apache Tiles and How to integrate tiles with Spring MVC web application. Before we start lets discuss about Why do we need to integrate tiles in our application ?. While creating an enterprise web application its very hard to implement same header and footer style throughout the application, writing same code for header and footer part again and again in every page is really a great pain for developers. Tiles provides a simple implementation of template designing using a external xml file.
In this blog we will create a simple hello world application using Spring MVC and Tiles frameworks. 

Project Structure
Here is a snapshot for overall project structure, add all required files to your project accordingly as mentioned in later part of this tutorial.

Libraries Used
We need to add some additional jar files to our project to make it working with tiles integration. Please make sure you have added all these jar files to your lib folder before start coding.

As we already knows tat web.xml is always a start point for our Web Application, it tell the container about application structure and behavior. We have added ebtries for DispatcherServlet to tell the container that all incoming request are going to be served by Spring itself. 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
    xmlns="" xmlns:web=""
    id="WebApp_ID" version="2.5">

This is the welcome file for our application, we have created an link to Home comtroller here so that further processing can be handed by spring.
<title>Being Java Guys |Tiles Integration</title>
        <br> <br> <br> <br> Being Java Guys |Tiles
        Integration Hello World <br> <br> <a href="hello.html">Click
            to see your first Tiles page !</a>

Spring Servlet is the core of Spring applicatons, all spring based configurations and setting goes here in a single file. In 'viewResolver' bean we have added an value of tiles2, indicating that view part of the application is going to be handled by Tiles framework itself.
In 'tilesConfigurer' bean an entry is added for tiles configuration file, tiles.xml. 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:p=""

    <context:component-scan base-package="com.beingjavaguys.controller" />

    <bean id="viewResolver"
        <property name="viewClass">
    <bean id="tilesConfigurer"
        <property name="definitions">

All tiles based configurations and settings goes here, we can specify a number of template here according to our need. These templates can be used along with different pages accordingly. Header, Footer and Other page components can be added here to template. 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
    <definition name="base.definition" template="/WEB-INF/jsp/Layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/jsp/Header.jsp" />
        <put-attribute name="menu" value="/WEB-INF/jsp/Menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/jsp/Footer.jsp" />

    <definition name="hello" extends="base.definition">
        <put-attribute name="title"
            value="Being Java Guys | Tiles Integration" />
        <put-attribute name="body" value="/WEB-INF/jsp/Hello.jsp" />


This is the controller class that takes care of all URL mappings and specified actions for the application.
package com.beingjavaguys.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class Home {

    public ModelAndView hello() {
        return new ModelAndView("hello", "message", "Body Content goes Here !");


In layout template files we can add page elements according to our need and in any design we want. There can be any number of such template configurations and files throughout the application. The <tiles:attribute > tags are used to add appropriate page component on required location.
<%@ taglib uri="" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" />
    <table border="1" cellspacing="2" align="center">
            <td height="30" colspan="2"><tiles:insertAttribute name="header" />
            <td height="250" width="150"><tiles:insertAttribute name="menu" />
            <td width="400"><tiles:insertAttribute name="body" />
            <td height="30" colspan="2"><tiles:insertAttribute name="footer" />


<p>Copyright &copy; Being Java Guys</p>

Here we are done with integrating spring web application with apache tiles framework. Just deploy your application on server and if everything goes right you will output something like these screens.

In this blog we came to know about, what is apache tiles framework and how to integrate Spring with Tiles framework. In upcoming blogs we will see some other tips and tricks about Spring Web Mvc Frameworks.

Thanks for reading !
Being Java Guys Team

Download "Spring Tiles HelloWorld Example" from "SkyDrive"


  1. thanks brother, its very useful thanks once again

  2. nice explanation

  3. Thanks you very much for this post . It was really helpfull for me.

  4. Hi,

    Thanks a lot for the post on Spring & Tiles integration!

    I am a beginner with Spring MVC. I wonder how to configure page views after adding few menu items in the menu. In the post there was nothing in the menu.jsp except a word "Menu" enter aligned. I hope you guys could guide me to add few menu items and corresponding controller, model and view configuration.

    Maria Sebastian

  5. Please send me maven dependencies.

  6. how to refresh only body tile without refreshing header menu footer tiles, send me with an example

  7. Muchas graciass!!


Like Us on Facebook

Like Us On Google+


Skype: neel4soft