Spring MVC and apache tiles 3 integration

Continue the post Spring, JPA and hibernate integration. In this tutorial I show you how to integration Spring MVC with Apache Tile 3.

Our target web layout as below







Prerequisite for this guide you need follow things:

  • Eclipse
  • Spring MVC
  • Apache Tile 3

1. Dependencies

We need add follow dependencies to pom.xml file


2. Configuring Spring MVC with Tiles

To make spring mvc work with tile we need add follow bean define to Spring DispatchServlet configuration file servlet-context.xml

<!-- Tile resolver -->
  <beans:bean id="tilesviewResolver"
    p:order="0" />

  <beans:bean id="tilesConfigurer"
    <beans:property name="definitions">

The order property equal 0 p:order="0" mean we want spring try to resolve view by tiles first.
We put tiles define file in folder /WEB-INF/tiles-defs/tiles-defs.xml.
So create this file and put following content into it

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
  <definition name="base" template="/WEB-INF/views/templates/template.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header"
      value="/WEB-INF/views/templates/header.jsp" />
    <put-attribute name="menu" value="/WEB-INF/views/templates/menu.jsp" />
    <put-attribute name="content" value="" />
    <put-attribute name="footer" value="/WEB-INF/views/templates/footer.jsp" />
  <definition name="add.emp" extends="base">
    <put-attribute name="title" value="Add Employee" />
    <put-attribute name="content" value="/WEB-INF/views/add.jsp" />

This file define a base template layout with header,menu,content and footer.
All pages with same layout can inherite from base template. In our sample Add employee page is extended from base layout.
3. Define template pages
Follow is content for template layout, header, menu and footer.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><tiles:insertAttribute name="title" /></title>
<style type="text/css">
#header {
  width: 100%;
  clear: both;
  border: solid 2px;

#menu {
  width: 18%;
  float: left;
  border: solid 2px;

#content {
  width: 80%;
  float: right;
  border: solid 2px;

#footer {
  width: 100%;
  clear: both;
  border: solid 2px;
  <div id="header">
    <tiles:insertAttribute name="header" />
  <div id="menu">
    <tiles:insertAttribute name="menu" />
  <div id="content">
    <tiles:insertAttribute name="content" />

  <div id="footer">
    <tiles:insertAttribute name="footer" />




Menu go here


<p>Copyright &#169; devjav.com</p>

4.Run application
Run application and go to url:http://localhost:8080/springhibernate/manage/add.do
The page will look as below:
5.Source code
You can download source code on my github or click this link

2 thoughts on “Spring MVC and apache tiles 3 integration”

  1. Pingback: URL

Leave a Reply