Javatpoint Logo

91-9990449935

 0120-4256464

Spring MVC Tiles Example

Spring provides integration support with apache tiles framework. So we can simply manage the layout of the spring mvc application by the help of spring tiles support.

Advantage of Tiles support in Spring MVC

  1. Reusability: We can reuse a single component in multiple pages like header and footer components.
  2. Centralized control: We can control the layout of the page by a single template page only.
  3. Easy to change the layout: By the help of single template page, we can change the layout of the page anytime. So your website can easily adapt new technologies such as bootstrap, jquery etc.

Spring MVC Tiles Example

Let's see a simple spring mvc tiles example.

Required Jar files

To run this example, you need to load:

  • Spring Core jar files
  • Spring Web jar files
  • Tiles jar files
download required jar file

You need to put these jar files inside the WEB-INF/lib directory.


Directory Structure

Let's see the files of spring tiles example in eclipse.

spring mvc tiles structure

Index Page

index.jsp

Controller classes

HelloWorldController.java
ContactController.java

Form class

Contact.java

web.xml

In this xml file, we are specifying the servlet class DispatcherServlet that acts as the front controller in Spring Web MVC. All the incoming request for the html file will be forwarded to the DispatcherServlet.

web.xml

spring-servlet.xml file

This is the important configuration file where we need to specify the ViewResolver and View components.

The context:component-scan element defines the base-package where DispatcherServlet will search the controller class.

Here, the UrlBasedViewResolver class is used for the ViewResolver.

The prefix+string returned by controller+suffix page will be invoked for the view component.

This xml file should be located inside the WEB-INF directory.

spring-servlet.xml

tiles.xml file

tiles.xml

View Components

hello.jsp
contact.jsp
header.jsp
footer.jsp
menu.jsp
layout.jsp

Output

spring mvc tiles 1 spring mvc tiles 2 spring mvc tiles 3