Spring 3 MVC: Tiles Plugin Tutorial with Example in Eclipse
- By Viral Patel on July 8, 2010
Welcome to Part 4 for Spring 3.0 MVC Series. In previous article we saw how to create a form using Spring 3 MVC and display it in JSP. Also we learn about annotation @ModelAttribute.
In this part we will discuss about Tiles Framework and its Integration with Spring 3.0 MVC. We will add Tiles support to our HelloWorld Spring application that we created in previous parts. I strongly recommend you to go through previous articles and download the source code of our sample application.
Spring 3.0 MVC Series
- Part 1: Introduction to Spring 3.0 MVC framework
- Part 2: Create Hello World Application in Spring 3.0 MVC
- Part 3: Handling Forms in Spring 3.0 MVC
- Part 4: Spring 3 MVC Tiles Plugin Tutorial with Example in Eclipse
- Part 5: Spring 3 MVC Internationalization & Localization Tutorial with Example in Eclipse
- Part 6: Spring 3 MVC Themes in Spring-Tutorial with Example
- Part 7: Create Spring 3 MVC Hibernate 3 Example using Maven in Eclipse
Introduction to Tiles 2
Nowadays, website are generally divided into pieces of reusable template that are being rendered among different web pages. For example a site containing header, footer, menu etc. This items remains same through out the website and give it a common look and feel. It is very difficult to hard code this in each and every webpage and if later a change is needed than all the pages needs to be modified. Hence we use templatization mechanism. We create a common Header, Footer, Menu page and include this in each page.
Tiles Plugin allow both templating and componentization. In fact, both mechanisms are similar: you
define parts of page (a “Tile”) that you assemble to build another part or a full page. A part can
take parameters, allowing dynamic content, and can be seen as a method in JAVA language. Tiles is a templating system used to maintain a consistent look and feel across all the web pages of a web application. It increase the reusability of template and reduce code duplication.
A common layout of website is defined in a central configuration file and this layout can be extended across all the webpages of the web application.
Our Application Layout
Our goal is to add Header, Footer and Menu to our Spring 3 HelloWorld application. Following will be the layout of the same.

Required JAR files
In order to add Tiles support to our Spring3 application, we will need few jar files. Following is the list of JARs in our example. Add these JARs in WEB-INF/lib folder.

The highlighted jar files in above list are the new jars to be added in project for Tiles integration.
Configuring Tiles framework in Spring MVC
To configure Tiles, an entry for bean TilesConfigure has to be made in spring-servlet.xml. Open the spring-servlet.xml from WEB-INF folder and add following code between <beans> </beans> tag.
File: /WebContent/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>
An input configuration file /WEB-INF/tiles.xml is passed as argument in above bean definition. This file contains the Tiles definition for our web application.
Create a file tiles.xml in WEB-INF folder and copy following code into it.

File: WebContent/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>
<definition name="contact" extends="base.definition">
<put-attribute name="title" value="Contact Manager" />
<put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
</definition>
</tiles-definitions>
Here in tiles.xml we have define a template base.definition. This layout contains attributes such as Header, Title, Body, Menu and Footer. The layout is then extended and new definitions for Contact page. We have override the default layout and changed the content for Body and Title.
Creating View – The JSPs

We will define the template for our webapplication in a JSP file called layout.jsp. This template will contain different segments of web page (Header, Footer, Menu etc). Create four new JSP files layout.jsp, header.jsp, menu.jsp and footer.jsp and copy following content in each of them.
File: WebContent/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>
File: WebContent/WEB-INF/jsp/header.jsp
<h1>Header</h1>
File: WebContent/WEB-INF/jsp/menu.jsp
<p>Menu</p>
File: WebContent/WEB-INF/jsp/footer.jsp
<p>Copyright © ViralPatel.net</p>
That’s All Folks
Compile and Execute the application in Eclipse and see that the header, menu and footer are properly applied.

Download Source Code
Click here to download Source Code (8.88kb).
Moving On
Today we saw how we can configure Tiles framework with Spring 3 MVC application. We used org.springframework.web.servlet.view.tiles2.TilesConfigurer class in bean definition to define the tiles configuration file. In next part we will discuss about Internationalization/Localization and adding its support in Spring 3 MVC. I hope you liked this article. Feel free to post your queries and comments in comment section.
Get our Articles via Email. Enter your email address.
Thanks Viral!
Thanks for all these tutorials =)
Perfect. Went very smooth.
Thanks Viral.
Sorry, doesn’t work for me.
When I enter http://localhost/layout.html in my browser it gives me a 404. Even when I use the code you provided.
(configured tomcat to listen on port 80, and not 8080, so the url is correct)
For me it’s give following exception :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’tilesConfigurer’ defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.apache.tiles.access.TilesAccess.setContainer(Lorg/apache/tiles/TilesApplicationContext;Lorg/apache/tiles/TilesContainer;Ljava/lang/String;)V
Good and simple: twice good.
Thanks !
Thanks for sharing your knowledge, great demo
I think here
must be
I’m wrong?
hi Viral,
completed all the things and have all the jars in palce but on starting server, getting below error
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.web.servlet.view.tile2.TilesConfigurer] for bean with name ’tilesConfigurer’ defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.web.servlet.view.tile2.TilesConfigurer
org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1250)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576)
org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1319)
org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:885)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
@Sepp
There is no Handler for “layout.html”. In tiles.xml we just configured how to build the v i e w “contact”. So the request is still the same as in the previous article of this Spring series (in my environment it’s “http://localhost:8080/Spring3MVC/” or “…/Spring3MVC/contacts.html”).
Hi Viral and all,
I am getting following error:
org.apache.tiles.template.NoSuchAttributeException: Attribute ‘header’ not found.
I have changed the ContactController to return layout instead of contacts
@RequestMapping(“/contacts”)
public ModelAndView showContacts() {
return new ModelAndView(“contact”, “command”, new Contact());
}
If I don’t change it gives me no error but without the template efffects.
I appreciate your help very much
Joy
im having this same issue – did you manage to solve it?
same issue as you, did you resolve it finally?
sorry there was a typo in my previous post. I have changed the Controller like this :
return new ModelAndView(“layout”, “command”, new Contact());
Thanks,
Sorry, there was a typo in my previous post. I have changed the Controller to:
return new ModelAndView(“layout”, “command”, new Contact());
Thanks
Howdy,
I have experienced with the “Dec 15, 2010 11:36:51 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/tdse/contacts.html] in DispatcherServlet with name ‘tdse’”, if I used this tutorial as it is but no luck.
Appreciate your help.
Hi,
When I implemented same thing, while running the application I get the error like,
java.lang.NullPointerException at org.apache.commons.digester.Digester.getXMLReader(Digester.java:1058) at org.apache.commons.digester.Digester.parse(Digester.java:1887) at org.apache.tiles.definition.digester.DigesterDefinitionsReader.read(DigesterDefinitionsReader.java:329) at org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO.loadDefinitionsFromURL(BaseLocaleUrlDefinitionDAO.java:276) at org.apache.tiles.definition.dao.CachingLocaleUrlDefinitionDAO.loadDefinitionsFromURLs(CachingLocaleUrlDefinitionDAO.java:251) at org.apache.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO.loadDefinitionsFromURLs(ResolvingLocaleUrlDefinitionDAO.java:65) at org.springframework.web.servlet.view.tiles2.TilesView.checkResource(TilesView.java:92) at org.springframework.web.servlet.view.UrlBasedViewResolver.loadView(UrlBasedViewResolver.java:421) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549) at javax.servlet.http.HttpServlet.service(HttpServlet.java:743) at javax.servlet.http.HttpServlet.service(HttpServlet.java:856) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.4.0) ].server.http.HttpRequestHandler.run(HttpRequestHandler.java:122) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.4.0) ].server.http.HttpRequestHandler.run(HttpRequestHandler.java:111) at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260) at com.evermind[Oracle Containers for J2EE 10g (10.1.3.4.0) ].util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303) at java.lang.Thread.run(Thread.java:619)
I am not able to solve it….if anyone knows what is the possible solution for that please let me know. This is driving me crazy…Any solution or workaround will do…thanks in advance.
Hi
When I enter http://localhost:8080/Spring3MVC in my browser it gives me HTTP Status 404 – /Spring3MVC/
but it works whene i enter http://localhost:8080/Spring3MVC/contacts.html
plz can u help me ?
@Pandel – you may want to add welcome-page in your web.xml to redirect request automatically to /Spring3MVC/contacts.html when user request for /Spring3MVC/. Add following to your web.xml:
<welcome-file-list> <welcome-file>contacts.html</welcome-file> </welcome-file-list>Thank you but it works except when I replace it with index.jsp
thank you for this tutorial
Thank for a really nice tutorial. I did followed the steps mentioned above. But not sure why i am getting this error
org.apache.tiles.jsp.taglib.NoSuchAttributeExcepti on: Attribute ‘header’ not found.
I googled around a lot but was not able to find a solution.
Appreciate your help
I am getting this kind of error please help me where i will get the jar files for tiles in spring 3
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’tilesConfigurer’ defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org.apache.tiles.startup.TilesInitializer
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:892)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory
Hi Viral,
Thanks for the great tutorials…
I tried the above – with the latest tiles but I am not getting any effects whatever…
Also while following this tutorial in sequence, I think there is some typo (contact – contact(s) ) – I fixed that but somehow no effect –
Will check up – and update here…
— cheerio atul
My mistake – I had not changed the viewResolver entry

Things work fine – as expected – moving to the next entry
— cheerio atul
I have experienced hard time to make it work:
Here is what changes I have made:
1. In web.xml file, I added this as Viral suggested. Question, there is no contacts.jsp (with s) file?
contacts.html
2. In sping-servlet.xml, I added this in the viewResolver tag:
/WEB-INF/jsp/
.jsp
When I run, I got the error
The requested resource (/spring3Tile/) is not available. spring3Title is my project name, also servlet name.
What else I need to do?
Thanks.
Oops, all tags are gone
Excelente, al principio no funcionaba, pero tenia mal el nombre del archivo tiles-defs.xml n.n
Saludos
Nice viral…Great work
100% works
My whole IT Project done with the help of your examples in this page..
are there anyone need the pom.xml
mail me
priyankahdp@gmail.com
little error in layout.jsp
should be
Can you also provide tutorial for using Struts2 + Spring3 + Tiles2 together. I tried using your tutorial “http://viralpatel.net/blogs/2009/12/struts-2-tiles-plugin-tutorial-with-example-in-eclipse.html” but it throws me error:
Unable to load configuration. – action – file:/C:/Tomcat6/webapps/marketing/WEB-INF/classes/struts.xml:16:60
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:371)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: Error building results for action welcome in namespace – action – file:/C:/Tomcat6/webapps/marketing/WEB-INF/classes/struts.xml:16:60
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
… 31 more
Caused by: There is no result type defined for type ’tiles’ mapped with name ‘success’. Did you mean ’tiles’? – result – file:/C:/Tomcat6/webapps/marketing/WEB-INF/classes/struts.xml:17:25
Hi,
I got the same issue, have you found the fix for this yet?
in the file spring-servlet.xml i add the tag that you say to add, but i have already a tag with the same id=”viewResolver”. so i remove the old one & it’s work.
but i don’t understand how it work without the tags :
can any one explain me …..
in the file spring-servlet.xml i add the tag that you say to add, but i have already a tag with the same id=”viewResolver”. so i remove the old one & it’s work.
but i don’t understand how it work without the tags :
-property name=”prefix” value=”/WEB-INF/jsp/”
-property name=”suffix” value=”.jsp”
can any one explain me …..
Hi Zouhayr, Viral and All,
I spent a longtime to get this example work, because I amended the previous step’s spring-context.xml configuration file by just changing the implementation class for the viewResolver from “org.springframework.web.servlet.view.JstlView” to “org.springframework.web.servlet.view.tiles2.TilesView” with attributes for prefix and suffix still remaining. I didn’t realise that these 2 attributes (prefix & suffix) must be removed. Upon, removing them it work, but it took time to realise that when adopting Tiles, the way that normal plain Spring MVC works, changes slightly. That is, in the normal case upon the Controller returning a String name for the View to be called, it will prefix the view name with prefix attribute and append the view name with suffix attribute as defined in the spring-context.xml configuration file to make up the full jsp name, e.g. /WEB-INF/jsp/contact.jsp. However, with Tiles, springframework will match the return String name from the Controller to the definition name as defined by tiles.xml configuation file, and then the Tiles implementation class will look up the matching definition name, e.g. ‘contact’ in that file, and then it looks at the body value to find the fully qualified jsp name, in our example here, it is /WEB-INF/jsp/contact.jsp. Dear Viral, I want to say that your tutorial is excellent, and if you could add my main points here to the tutorial, I’m sure many readers will benefit. Regards, Bolat
To clarify here, by stating that you are using tiles spring will look in tiles.xml for your views and won’t resolve them automatically to jsps as per the previous tutorial (because those two properties have to be removed).
So in tiles.xml, add:
where can i download those jars? i am currently using spring tool suite
leon, you can download all jars from maven repository: http://mvnrepository.com
can u show me one example of spring 3.0 web mvc which use 3 jsp
1)index
2)login
3) registration
and all this be done using xml..i dont want to use annotation..plz
i search a lot but not found any help..
I got this error:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:519)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.tiles.template.NoSuchAttributeException: Attribute ‘header’ not found.
org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:49)
org.apache.tiles.template.InsertAttributeModel.resolveAttribute(InsertAttributeModel.java:187)
org.apache.tiles.template.InsertAttributeModel.start(InsertAttributeModel.java:107)
org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:306)
org.apache.jsp.layout_jsp._jspx_meth_tiles_005finsertAttribute_005f0(layout_jsp.java:104)
org.apache.jsp.layout_jsp._jspService(layout_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Hey , nice guide , but there are still some questions that remains unclear ,
* what about the Controler?
*A control which has a model(“aString,”demo”) and sending us to a template-view , all the jsp in that template share information? all have access to aString?
*Can/How Menu “talk” to Body for example?
*You didn’t mention that jspViewResolver is no needed anymore
Waiitng for you reply , thanks
I have found another amazing exmaple
http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html
I just tested the example using the latest available jar files.
1. Do not use commons-digester3-3.0.jar with tiles-…-2.2.2.jar
You will have an error:
java.lang.NoClassDefFoundError: org/apache/commons/digester/Rule
Use instead commons-digester-2.1.jar
2. The lastest Tomcat version contents jstl.jar under Tomcat/lib/
So there is no need to add jstl-api-1.2.jar and jstl-impl-1.2.jar
Hi,
In the previous example
Part 3: Handling Forms in Spring 3.0 MVC :
in spring-servlet.xml you put :
Now in spring-servlet.xml you put :
org.springframework.web.servlet.view.tiles2.TilesView
My question please is :
How if I want to use the handling a form inside Tiles ?
Thanks, your help is appreciated.
It’s better to add Jar named “tiles-jsp-2.1.2.jar” also.
BTW, thanks for this wonderful tutorial. It helped me a lot.
Simple and clear… Could the contents be dynamic? Can we make tiles – parameterised?
Hello I got this error!!
Schwerwiegend: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’tilesConfigurer’ defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/digester/Rule
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
—-
Solution to this error: replace commons-digester3-3.0.jar with commons-digester-2.1.jar. Download from:
http://mirror.candidhosting.com/pub/apache//commons/digester/binaries/commons-digester-2.1-bin.zip
Thanks
It doesn’t work for me. I got this exception :-
SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’tilesConfigurer’ defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: Class org.apache.tiles.web.util.ServletContextAdapter not recognized a TilesApplicationContext
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsupportedOperationException: Class org.apache.tiles.web.util.ServletContextAdapter not recognized a TilesApplicationContext
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
Hi Viral,
excellent tuto!!
may I suggest to complete the project with a source-folder called configuration and write in it
lod4j.xml :
this will avoid Warning in the console
Excellent tutorial. I found various issues with this part of the tutorial, but primarily it was my fault. For others, I will recommend please ensure that you have correct version of tiles*.jar file and tiles-jsp.tld file. I put in the latest versions or incompatible versions and got into lot of issues. Finally, getting the correct versions as specified in tutorial got it right.
contacts.html