Spring MVC Tiles Integration Example

 

Apache Tiles is a templating framework used in MVC applications to simplify the development of user interfaces.

It allows to define page fragments which are common to all application pages, we can then include these fragments to any other page or use them to create a reusable template.

In this example, we will see how to integrate Tiles framework into Spring MVC web application.

 

1. Technologies used

  • Spring 3.2.8.RELEASE
  • Tiles 3.0.5
  • Maven 3.0.5
  • JDK 7.0
  • Eclipse 4.4
  • Tomcat 7.0

 

2. Project structure

We create a web application project.

SpringMVC-Tiles-Integration

 

3. Project dependencies

We need to add Spring and Tiles dependencies to our project’s pom.

File : pom.xml

 

4. Create the template

We will create a template that has the following structure :

SpringMVC-Tiles-Integration-3

 

File : template.jsp

The tag  <tiles:insertAttribute ../>  is used to create page fragments.

We add some style to our template :

File : style.css

 

5. Create the composing pages

For each attribute in our template, we create a JSP page (fragment) : header, body, menu and footer

File : header.jsp

File : body.jsp

File : menu.jsp

File : footer.jsp

 

6. Create tiles definition

We will use an XML file configuration to define the template core :

File : tiles.jsp

 

7. Use the template

Now we can use our template to create two JSP pages :

page1.jsp : just render the template

 

page2.jsp : customize the body and the footer fragments

 

8. Controller

File : ViewController

 

9. Spring MVC Configuration

File : mvc-dispatcher-servlet.xml

 

10. Test It

URL : http://localhost:8080/SpringMVC-Tiles-Integration/page1.htm

SpringMVC-Tiles-Integration-1

 

URL : http://localhost:8080/SpringMVC-Tiles-Integration/page2.htm

SpringMVC-Tiles-Integration-2

In the second page, we can see that the body and the footer have been changed.

 

Download source code

 

  • Gustavo Minoru

    You forgot the web.xml

    App Name

    mvc-dispatcher

    org.springframework.web.servlet.DispatcherServlet

    1

    mvc-dispatcher

    *.htm