Spring MVC Tiles 3 Integration | Spring Tiles Integration

Keeping you updated with latest technology trends, Join DataFlair on Telegram

1. Objective

In our previous tutorial, we studied Spring Security. Today in this Spring MVC Tiles 3 Integration tutorial, we will learn about the integration of Spring MVC Framework with Apache Tiles3. Along with that, we will see the advantages of Spring Tiles Integration it and the working example with Eclipse IDE in place.
So, let’s start Spring MVC Tiles 3 Integration.

Spring MVC Tiles 3 Integration | Spring Tiles Integration

Spring MVC Tiles 3 Integration | Spring Tiles Integration

2.Spring MVC Tiles 3

Before going through this Spring MVC Tiles 3 Integration tutorial, we should have basic knowledge of Java language and Spring Framework. Apache Tiles is an open source framework. It is a template engine for Java-based web frameworks.
One of the major areas where the Spring MVC Framework has advanced comparing it with other frameworks is in a separation of the view technologies. The Apache Tiles is based on a composite pattern which is used for simplifying the development of UIs. The Spring Framework provides the integration support with the Apache Tiles framework so as to develop required web applications and manage the layout of Spring MVC by the help of Spring Tiles.

3. Advantages of Tiles in Spring MVC

Some of the major advantages of Spring MVC Tiles 3 Integration as follows:

a. Reusability

You can reuse a single component in many pages. For example, header and footer components.

b. Centralised Control

You have the ability to control the layout of the page. This is done using the single template page only.
c. Easily Changeable Layout
Using the single template page, you are capable of changing the layout of the page anytime. Therefore, your website becomes adaptable to new technologies such as jquery, bootstrap etc.
Read about Spring BeanPostProcessors – Latest Tutorial 2018

4. Spring MVC Tiles Example

Before you proceed with Integration of Spring MVC Tiles 3, we will discuss the working example you need to load some of the jar files:
Spring Core jar files
Spring Web jar files
Tiles jar files
These jar files should be put inside the WEB_INF/lib directory.
Let’s define a working example using Eclipse IDE having the following steps:

  1. Create your project with name SpringEx and a package com.example. This should be under the src folder of your created project.
  2. Add the Spring Libraries that are required using the Add External JARs options.
  3. Define an index page with index.jsp.
  4. Create HelloWorldController.java, ContactController.java, and Contact.java under the above-made package.
  5. Write the web.xml, tile.xml and spring-servlet.xml configuration file under the src folder.
  6. Write the code for all the View components.
  7. Finally, write code for all Java files and Bean config file and run the application as described.

a. Index Page


<a href="hello.html">Hello Spring</a> |   
<a href="contact.html">Contact</a>

b. Controller classes


package com.example.controller;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.servlet.ModelAndView;  
public class HelloWorldController {  
    public ModelAndView helloWorld() {  
        String message = "Hello World, Spring MVC @ example";  
        return new ModelAndView("hello", "message", message);  


package com.example.controller;  
import org.springframework.stereotype.Controller;  
import org.springframework.validation.BindingResult;  
import org.springframework.web.bind.annotation.ModelAttribute;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.bind.annotation.SessionAttributes;  
import org.springframework.web.servlet.ModelAndView;  
import com.example.form.Contact;  
public class ContactController {  
   @RequestMapping(value = "/addContact", method = RequestMethod.POST)  
    public String addContact(@ModelAttribute("contact") Contact contact,BindingResult result)        return "redirect:contact.html";  
    public ModelAndView showContacts() {  
        return new ModelAndView("contact", "command", new Contact());  

Read about Spring Bean Definition Inheritance & Bean Definition Template

c. Form class


package com.example.form;  
public class Contact {  
    private String firstname;  
    private String lastname;  
    private String email;  
    private String telephone;  

d. web.xml


<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   

e. spring-servlet.xml file

This is the important configuration file where you need to specify the ViewResolver and View components.
This XML file should be located inside the WEB-INF directory.

 <beans xmlns="http://www.springframework.org/schema/beans"  
        base-package="com.example.controller" />    
    <bean id="viewResolver"  
        <property name="viewClass">  
    <bean id="tilesConfigurer"  
        <property name="definitions">  

f. tiles.xml file


<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE tiles-definitions PUBLIC  
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" 
    <definition name="base.definition"  
        <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="contact" extends="base.definition">  
        <put-attribute name="title" value="Contact Manager" />  
        <put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />  
    <definition name="hello" extends="base.definition">  
        <put-attribute name="title" value="Hello Spring MVC" />  
        <put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" />  

Let’s Read Spring Java Based Configuration – How to Configure Spring Beans

g. View Components


    <title>Spring MVC Example</title>  
<h1>Welcome to Spring MVC</h1>  
    <p>Message is: ${message}</p>  


<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <title>Spring Tiles Contact Form</title>  
<h2>Contact Manager</h2>  
<form:form method="post" action="addContact.html">  
        <td><form:label path="firstname">First Name</form:label></td>  
        <td><form:input path="firstname" /></td>   
        <td><form:label path="lastname">Last Name</form:label></td>  
        <td><form:input path="lastname" /></td>  
        <td><form:label path="lastname">Email</form:label></td>  
        <td><form:input path="email" /></td>  
        <td><form:label path="lastname">Telephone</form:label></td>  
        <td><form:input path="telephone" /></td>  
        <td colspan="2">  
            <input type="submit" value="Add Contact"/>  




<p>Copyright  example.com.</p>


<p>Menu 1</p>  
<p>Menu 2</p>

Do you know Spring Dependency Injection – Types of Spring DI & Example

<%@ taglib uri="http://tiles.apache.org/tags-tiles" 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" /></title>  
        <div><tiles:insertAttribute name="header" /></div>  
        <div style="float:left;padding:10px;width:15%;"><tiles:insertAttribute name="menu" /></div>  
        <div style="float:left;padding:10px;width:80%;border-left:1px solid pink;">  
        <tiles:insertAttribute name="body" /></div>  
        <div style="clear:both"><tiles:insertAttribute name="footer" /></div>  

If you have made all of the above files then run the project. You will see a message with two Sub Menus as output:
Welcome to Spring MVC
Clicking on the Sub Menu you will see a form where you have to fill the contact details.
So, this was all about Spring MVC Tiles 3 Integration. Hope you like our explanation.

5. Conclusion

Hence, in this Spring MVC Tiles 3 Integration Tutorial, we have seen about the integration of Spring Framework MVC with Apache Tiles Framework. Along with that you saw advantages of the Spring Tiles with a working example using Eclipse IDE. Furthermore, if you have any query regarding Spring MVC Tiles 3 Integration, feel free to ask in the comment section.
See Also- Spring Batch
For reference

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.