DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Spring Tiles Integration Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (1)
Save
Tweet
76.17K Views

Join the DZone community and get the full member experience.

Join For Free

in this example you will learn how to integrate spring with tiles 2. the directory structure of the example is shown below.

add the following library files to the lib directory.

antlr-runtime-3.0
commons-logging-1.0.4
org.springframework.asm-3.0.0.m3
org.springframework.beans-3.0.0.m3
org.springframework.context-3.0.0.m3
org.springframework.context.support-3.0.0.m3
org.springframework.core-3.0.0.m3
org.springframework.expression-3.0.0.m3
org.springframework.web-3.0.0.m3
org.springframework.web.servlet-3.0.0.m3

commons-beanutils-1.7.0
commons-digester-1.8
commons-logging-api-1.1
jstl
standard
tiles-api-2.0.4
tiles-core-2.0.4
tiles-jsp-2.0.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="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemalocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">
	
	<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.tiles2. 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="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="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.tiles2.tilesview
welcome.url=welcome

friends.(class)=org.springframework.web.servlet.view.tiles2.tilesview
friends.url=friends

office.(class)=org.springframework.web.servlet.view.tiles2.tilesview
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.tiles2. 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 a org.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 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>

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 the welcome 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")
	public string redirect()
	{
		return "welcome";
	}
}

you can download and try the example here.

source : download
war : download
Spring Framework Integration

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 6 Things Startups Can Do to Avoid Tech Debt
  • What Is Lean Software Development
  • Real-Time Supply Chain With Apache Kafka in the Food and Retail Industry
  • How to Utilize Python Machine Learning Models

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo