SpringMVC 3 + Tiles 2.2.2 Integration
Apache Tiles is a popular and mostly used templating framework for java based web application.
Tiles became more popular because Struts 1.x uses Tiles as its default templating framework.
SpringMVC which is an MVC framework, like Struts, also supports integration of Tiles as its templating framework.
Let us see how we can integrate SpringMVC and Tiles.
You can download Tiles binaries from http://tiles.apache.org/ .
Step#1: Add the following tiles jars to WEB-INF/lib folder.
tiles-api-2.2.2.jar
tiles-core-2.2.2.jar
tiles-jsp-2.2.2.jar
tiles-servlet-2.2.2.jar
tiles-template-2.2.2.jar
Step#2: Configure tiles integration in WEB-INF/dispatcher-servlet.xml
03 | < bean id = "tilesConfigurer" class = "org.springframework.web.servlet.view.tiles2.TilesConfigurer" > |
04 | < property name = "definitions" > |
06 | < value >/WEB-INF/tiles.xml</ value > |
11 | < bean id = "viewResolver" class = "org.springframework.web.servlet.view.UrlBasedViewResolver" > |
12 | < property name = "viewClass" value = "org.springframework.web.servlet.view.tiles2.TilesView" /> |
Step#3: Configure tiles definitions in WEB-INF/tiles.xml
06 | < definition name = "baseLayout" template = "/jsp/layout/layout.jsp" > |
07 | < put-attribute name = "title" value = "SivaLabs" /> |
08 | < put-attribute name = "header" value = "/jsp/layout/header.jsp" /> |
09 | < put-attribute name = "navigation" value = "/jsp/layout/navigation.jsp" /> |
10 | < put-attribute name = "body" value = "" /> |
11 | < put-attribute name = "footer" value = "/jsp/layout/footer.jsp" /> |
14 | < definition name = "login" extends = "baseLayout" > |
15 | < put-attribute name = "title" value = "SivaLabs : Login" /> |
16 | < put-attribute name = "navigation" value = "" /> |
17 | < put-attribute name = "body" value = "/jsp/login.jsp" /> |
20 | < definition name = "welcome" extends = "baseLayout" > |
21 | < put-attribute name = "title" value = "SivaLabs : Welcome" /> |
22 | < put-attribute name = "body" value = "/jsp/welcome.jsp" /> |
Step#4: Code the layout JSPs
layout.jsp
04 | < title >< tiles:insertAttribute name = "title" ignore = "true" /></ title > |
05 | < script type = "text/javascript" src = "js/sivalabs.js" ></ script > |
09 | < table border = "1" style = "border-collapse: collapse;" cellpadding = "2" cellspacing = "2" align = "center" width = "800" > < tbody >< tr > |
10 | < td height = "30" colspan = "2" >< tiles:insertAttribute name = "header" /></ td > |
13 | < td width = "150" height = "450" valign = "top" > |
15 | < tiles:insertAttribute name = "navigation" /> |
18 | < td valign = "top" width = "650" > |
20 | < tiles:insertAttribute name = "body" /> |
25 | < td height = "30" colspan = "2" > |
27 | < tiles:insertAttribute name = "footer" /> |
31 | </ tbody ></ table ></ body > |
header.jsp
1 | < h2 >SivaLabs : My Experiments On Technology</ h2 > |
footer.jsp
2 | < b >© 2011 SivaLabs All Rights Reserved</ b > |
navigation.jsp
1 | < p >< a href = "createUser.do" >Create User</ a ></ p >< p >< a href = "listUsers.do" >View Users</ a ></ p >< p >< a href = "logout.do" >Logout</ a ></ p > |
welcome.jsp
1 | < h2 >Welcome to SpringMVC+Tiles Sample Application </ h2 > |
Step#5:
WelcomeController.java
01 | package com.sivalabs.web.controllers; |
03 | import org.springframework.stereotype.Controller; |
04 | import org.springframework.web.bind.annotation.RequestMapping; |
07 | public class WelcomeController |
09 | @RequestMapping ( "welcome" ) |
10 | public String welcome() |
Here the String "welcome" will be resolved as a tile name and display the UI as per "welcome" tile configuration.