facebook twitter youtube
by Amit Gupta - no comments

Tutorials in this series

  1. Quick jump with Spring 3 MVC – Theory apart : Covers spring MVC project setup
  2. Spring 3 MVC after a quick jump : Covers require spring annotations you use in controller, comparison with JAX-RS annotations, and many other request identifiers
  3. Basic Form Handling : How to handle a request and map it to a controller’s method. More about Model.
  4. Advanced Form handling : Handling multiple rows, binding multiple beans to a single form, uploading files etc.
  5. Basic Form validations
  6. Advanced Form Validation : custom validator and validating date field.

Why Tiles?

While developing a website you will notice many areas are repeated across all pages, like: header, footer, menu, contacts etc. You can design separate pages for them and can insert them in contents of other pages. You can call them as master pages as well.
Tiles provide a mechanism for this task. First you need to design a layout page where you can specify location of master pages and contents as below.

WEB-INF/jsp/layout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<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>

Somewhere you need to specify values for tiles attributes you used in above page.

WEB-INF/tiles.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="base.definition"
        template="/WEB-INF/jsp/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
        <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
    </definition>
  
</tiles-definitions>

Each entry in tiles.xml is called definition. You can inherit a definition by another.

    <definition name="contact" extends="base.definition">
        <put-attribute name="title" value="Contact Manager" />
        <put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
    </definition>

Attributes of contact definition will overwrite attribute’s values of base.definition.

Of course, you need to design all jsp pages.

Don’t forget to tell spring that you are using tiles framework.

WEB-INF/spring-servlet.xml

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass">
        <value>
            org.springframework.web.servlet.view.tiles2.TilesView
        </value>
    </property>
</bean>
<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

source: viralpatel.net

However the original article written by Patel is very much explanatory, I just rewrote it in my way on one of the friend’s request.

Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

Leave a Reply

captcha