About Siva Reddy

Katamreddy Siva Prasad is a Senior Software Engineer working in E-Commerce domain. His areas of interest include Object Oriented Design, SOLID Design principles, RESTful WebServices and OpenSource softwares including Spring, MyBatis and Jenkins.

SpringMVC 3 Tiles 2.2.2 Integration Tutorial

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 here

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

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

Step#3: Configure tiles definitions in WEB-INF/tiles.xml

<!--?xml version="1.0" encoding="UTF-8"?-->
 
  
<tiles-definitions>
 
 <definition name="baseLayout" template="/jsp/layout/layout.jsp">
  <put-attribute name="title" value="SivaLabs" />
  <put-attribute name="header" value="/jsp/layout/header.jsp" />
  <put-attribute name="navigation" value="/jsp/layout/navigation.jsp" />
  <put-attribute name="body" value="" />
  <put-attribute name="footer" value="/jsp/layout/footer.jsp" />
 </definition>
  
 <definition name="login" extends="baseLayout">
  <put-attribute name="title" value="SivaLabs : Login" />
  <put-attribute name="navigation" value="" />
  <put-attribute name="body" value="/jsp/login.jsp" />
 </definition>
   
 <definition name="welcome" extends="baseLayout">
  <put-attribute name="title" value="SivaLabs : Welcome" />
  <put-attribute name="body" value="/jsp/welcome.jsp" />
 </definition>
   
</tiles-definitions>

Step#4: Code the layout JSPs

layout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<title><tiles:insertAttribute name="title" ignore="true" /></title>
<script type="text/javascript" src="js/sivalabs.js"></script>
</head>
<body>
 
<table border="1" style="border-collapse: collapse;" cellpadding="2" cellspacing="2" align="center" width="800">    <tbody><tr>
        <td height="30" colspan="2"><tiles:insertAttribute name="header" /></td>
    </tr>
    <tr>
        <td width="150" height="450" valign="top">
 
         <tiles:insertAttribute name="navigation" />
 
        </td>
        <td valign="top" width="650">
 
         <tiles:insertAttribute name="body" />
 
        </td>
    </tr>
    <tr>
        <td height="30" colspan="2">
 
         <tiles:insertAttribute name="footer" />
 
        </td>
    </tr>
</tbody></table></body>
</html>

header.jsp

<h2>SivaLabs : My Experiments On Technology</h2>

footer.jsp

<center>
 <b>© 2011 SivaLabs All Rights Reserved</b>
</center>

navigation.jsp

<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

<h2>Welcome to SpringMVC+Tiles Sample Application </h2>

Step#5:

WelcomeController.java

package com.sivalabs.web.controllers;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class WelcomeController
{
 @RequestMapping("welcome")
 public String welcome()
 {
  return "welcome";
 }
}

Here the String “welcome” will be resolved as a tile name and display the UI as per “welcome” tile configuration.

Happy coding
Byron
Related Articles :
Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

8 Responses to "SpringMVC 3 Tiles 2.2.2 Integration Tutorial"

  1. blessbon says:

    Caused by: org.apache.tiles.definition.DefinitionsFactoryException: Cannot load definition URLs
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer$SpringTilesContainerFactory.getSourceURLs(TilesConfigurer.java:428)
    at org.apache.tiles.factory.BasicTilesContainerFactory.createLocaleDefinitionDao(BasicTilesContainerFactory.java:298)
    at org.apache.tiles.factory.BasicTilesContainerFactory.createDefinitionsFactory(BasicTilesContainerFactory.java:242)
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer$SpringTilesContainerFactory.createDefinitionsFactory(TilesConfigurer.java:481)
    at org.apache.tiles.factory.BasicTilesContainerFactory.createContainer(BasicTilesContainerFactory.java:104)
    at org.apache.tiles.startup.AbstractTilesInitializer.createContainer(AbstractTilesInitializer.java:124)
    at org.apache.tiles.startup.AbstractTilesInitializer.initialize(AbstractTilesInitializer.java:70)
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer.afterPropertiesSet(TilesConfigurer.java:339)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    … 22 more
    Caused by: java.io.FileNotFoundException: ServletContext resource [/
    /WEB-INF/tiles.xml
    ] cannot be resolved to URL because it does not exist
    at org.springframework.web.context.support.ServletContextResource.getURL(ServletContextResource.java:154)
    at org.springframework.web.servlet.view.tiles2.SpringTilesApplicationContextFactory$SpringWildcardServletTilesApplicationContext.getResources(SpringTilesApplicationContextFactory.java:105)
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer$SpringTilesContainerFactory.getSourceURLs(TilesConfigurer.java:423)
    … 31 more

  2. Barney says:

    Hi all!

    Thanks for the post. It is works for me very well.

    But How can I display dynamic data in a head/footer? For example I would like to display the user’s unread message number, and some information whick come from the database in the Head tile?

    It is a bad design that assign in every controller this information, because in every controller I must do that, and this is not manageable!

    Is there any way for the ReversePresenter Pattern (Like in GoogleWebToolkit – every view known there own presenter, and can ask data from it)?

    Or how other way can I assign dynamic information in a tile, which always show?

    Thanks
    Barney

  3. For those getting the above exception regarding tile factory, pls. see if my tutorial on spring 3 mvc and tiles works for you.

  4. In step#3 i found that i had to add this tag

  5. Robert Johnson says:

    Please help me out with this error

    HTTP Status 404 –

    type Status report

    message

    description The requested resource () is not available.

    Apache Tomcat/6.0.32

  6. Michael De Keyser says:

    Wait… Do you have to add a definition in tiles.xml for EVERY page there is?!

  7. taktouk says:

    i have a probleme with tiles
    org.apache.tiles.jsp.taglib.NoSuchAttributeException: Attribute ‘header’ not found.
    i put the same configuration
    plz help !!!!

Leave a Reply


eight − 8 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books