In this example you
will learn how to integrate Spring with Tiles The directory structure of the example is
shown below.
Add the following
library files to the lib directory.
antlr-runtime-0
commons-logging-4
org.springframework.asm-M3
org.springframework.beans-M3
org.springframework.context-M3
org.springframework.context.support-M3
org.springframework.core-M3
org.springframework.expression-M3
org.springframework.web-M3
org.springframework.web.servlet-M3
commons-beanutils-0
commons-digester-8
commons-logging-api-1
jstl
standard
tiles-api-4
tiles-core-4
tiles-jsp-4
You will see how to
create a simple classic tile layout with a header, menu, body and footer
regions.
In Spring to use
Tiles, configure the following Tile definition in the Spring configuration
file.
<?xml version="0" encoding="UTF-8"?>
xsi:schemaLocation="
<bean id="viewResolver" class="org.springframework.web.servlet.view.
ResourceBundleViewResolver" p:basename="views" />
<context:component-scan base-package="com.vaannila.web" />
<bean id="tilesConfigurer"class="org.springframework.web.servlet.view.tiles
TilesConfigurer"p:definitions="/WEB-INF/tiles-defs.xml" />
</beans>
Using the definitions attribute specify the location of the tiles definition file. Here the
location is "/WEB-INF/tiles-defs.xml". The tiles
definition file is shown below.
<?xml version="0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software
Foundation//DTD Tiles Configuration 0//EN"
<tiles-definitions>
<definition name="baseLayout" template="/WEB-INF/tiles/baseLayout.jsp">
<put-attribute name="title" value="Template"/>
<put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/>
<put-attribute name="menu" value="/WEB-INF/tiles/menu.jsp"/>
<put-attribute name="body" value="/WEB-INF/tiles/body.jsp"/>
<put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp"/>
</definition>
<definition name="welcome" extends="baseLayout">
<put-attribute name="title" value="Welcome"/>
<put-attribute name="body" value="/WEB-INF/jsp/welcome.jsp"/>
</definition>
<definition name="friends" extends="baseLayout">
<put-attribute name="title" value="Friends"/>
<put-attribute name="body" value="/WEB-INF/jsp/friends.jsp"/>
</definition>
<definition name="office" extends="baseLayout">
<put-attribute name="title" value="Office"/>
<put-attribute name="body" value="/WEB-INF/jsp/office.jsp"/>
</definition>
</tiles-definitions>
Here we first
define a base layout, later we extend the base layout and create three more
tile definitions by changing only the title and the body regions.
To dispaly views we use the ResourceBundleViewResolver. By default the views.properties file will be used to store the key value pairs, we specify this using
the basename attribute.
welcome.(class)=org.springframework.web.servlet.view.tilesTilesView
welcome.url=welcome
friends.(class)=org.springframework.web.servlet.view.tilesTilesView
friends.url=friends
office.(class)=org.springframework.web.servlet.view.tilesTilesView
office.url=office
about.(class)=org.springframework.web.servlet.view.JstlView
about.url=/WEB-INF/jsp/about.jsp
The welcome, friends and office refers to the tile definition names (the one to right side of the =
sign). For tile views we use "org.springframework.web.servlet.view.tiles TilesView" class. You
can also have other views together with the tile views. The about url is mapped to the about.jsp page with is aorg.springframework.web.servlet.view.JstlView.
The baseLayout.jsp
file contains the table structure to hold the different regions.
<%@ taglib uri="http://tiles.apache.org/tags-tiles"
prefix="tiles" %>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 01 Transitional//EN"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>
<td height="30" colspan="2">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr>
<td height="250">
<tiles:insertAttribute name="menu" />
</td>
<td width="350">
<tiles:insertAttribute name="body" />
</td>
</tr>
<tr>
<td height="30" colspan="2">
<tiles:insertAttribute name="footer" />
</td>
</tr>
</table>
</body>
</html>
Here we use annotated controller
handler mapping to handle the request. In the redirect.jsp page we forward the request to the welcome.htm url.
<% response.sendRedirect("welcome.htm"); %>
The welcome.htm url will be handled by the WelcomeController class, where we forward it to thewelcome tile page.
package com.vaannila.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WelcomeController {
@RequestMapping("/welcome.htm")
{
return "welcome";
}
}
No comments:
Post a Comment