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 StructureHere is a snapshot for overall project structure, add all required files to your project accordingly as mentioned in later part of this tutorial.

Libraries UsedWe 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.

/WebContent/WEB-INF/web.xmlAs 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.

/WebContent/WEB-INF/spring-servlet.xml
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.

/WebContent/WEB-INF/tiles.xmlAll 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.

/WebContent/WEB-INF/jsp/Layout.jspIn 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.

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.

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.