Sunday 17 March 2013

Spring MVC with Apache Tiles 3

There is a dearth of tutorials which examine Spring MVC with Apache Tiles 3. Most of them allude to this one, which is insufficient. Having recently integrated Tiles 3 into my Spring MVC project, I have decided to make a short tutorial on how to get everything running.

What you need : Apache Tiles 3, Spring Framework 3.2.2 (earlier versions may not support Tiles 3 by default), Spring MVC

Steps :

Add your Tiles dependency to pom.xml (or download the Tiles 3 jar and add to your classpath if you're not using Maven) :
Make sure you're using Spring 3.2.2 :
In your servlet-context.xml (or whatever you named yours) :
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <!--Don't add suffix or prefix like you do with .jsp files-->
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" >
    <property name="definitions">
If you were using the traditional Spring MVC view resolution method with jsp files, your servlet-context.xml might have had something like this :
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
Please remove that, since you're using a new mechanism to resolve views.

Create a tiles.xml file under your WEB-INF directory :
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "">


    <definition name="*/*" template="/WEB-INF/tiles/template.jsp">
        <put-attribute name="meta" value="/WEB-INF/tiles/{2}/meta.jsp" />
        <put-attribute name="header" value="/WEB-INF/tiles/{2}/header.jsp" />
        <put-attribute name="body" value="/WEB-INF/tiles/{2}/body.jsp" />
        <put-attribute name="footer" value="/WEB-INF/tiles/{2}/footer.jsp" />


Here's the directory structure :

Create a template.jsp under WEB-INF/tiles. Here's an example :
<%@ taglib prefix="tiles" uri="" %>

        <tiles:insertAttribute name="meta" />
        <div id="header">
            <tiles:insertAttribute name="header" />
        <div id="body">
            <tiles:insertAttribute name="body" />
        <div id="footer">
            <tiles:insertAttribute name="footer" />
Whatever java controller you're using, make sure that the view you're returning matches the wildcard pattern in tiles.xml e.g., "/index" will match "*/*" in tiles.xml and you can use {2} to get the "index" string and tiles will render the tiles under the tiles/index folder :
public class IndexController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView indexHandler() {
        return new ModelAndView("/index", "user", new SomeObject());
Lastly, you can add your views under the WEB-INF/tiles directory. Remember that you'll need the same jsp files which are described in your tiles.xml file.

Extra :

If you require list attribute inheritance (which was implemented as a "fallback" here, and did not work for me at all), then change your tiles.xml to :
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "">


    <definition name="base" template="/WEB-INF/tiles/template.jsp">
        <put-attribute name="meta" value="/WEB-INF/tiles/common/meta.jsp" />
        <put-attribute name="footer" value="/WEB-INF/tiles/common/footer.jsp" />

    <definition name="*/*" extends="base">
        <put-attribute name="header" value="/WEB-INF/tiles/{2}/header.jsp" />
        <put-attribute name="body" value="/WEB-INF/tiles/{2}/body.jsp" />

What this essentially does is use the jsp files from the common folder, so you can avoid copying and pasting the same jsp file over and over again in all your subfolders. My directory structure now looks like this :
Notice that I don't need to have the footer.jsp and meta.jsp in all the WEB-INF/tiles subfolders.


  1. Hello Dhruv..Thank you very much for the example.
    I followed same steps but I am getting below error when start my application..can you please help me with htis?

    java.lang.ClassNotFoundException: org.apache.tiles.ArrayStack

    1. That class is not included with Tiles 3. I think you're using Tiles 2.

    2. Thank you for your reply. I double checked all files, nowhere I found tiles2. I have tiles3 jars in lib too.

      I hope you can understand below code.



      !DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" ""

      Here is the error I am getting ( couldn’t paste all)
      org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.tiles.request.render.CannotRenderException: ServletException including path '/WEB-INF/jsp/layout.jsp'.
      org.apache.tiles.request.render.CannotRenderException: ServletException including path '/WEB-INF/jsp/layout.jsp'.
      root cause
      javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/tiles/ArrayStack

    3. Sorry..dispatcher-servlet text didn't show

      Tiles Inegration -->
      bean id="tilesConfigurer"

      bean id="viewResolver"
      property name="viewClass"

  2. Maybe you should make a new stackoverflow question on it. I think your tiles.xml is probably configured wrongly. Can you try removing "/" from "/WEB-INF/jsp/layout.jsp" so that it looks like "WEB-INF/jsp/layout.jsp" inside your tiles.xml. Again, I would recommend that you create a new stackoverflow question.

    1. Thank you Dhruv. Sure I will try that and see, else I will create new one.

  3. Could you send use the source ? doesn't work for me :(

  4. This comment has been removed by the author.

  5. Ever get the fallback working? I've tried all day and keep getting this exception from the OptionsRenderer:

    java.lang.IllegalStateException: A matching list-attribute name="my_opts" must be defined.

    I put the list attribute in the definitions and referenced it according to the docs, but it doesn't work. Any luck? Here's a snippet of my code in tiles.xml within a definition element:

    put-attribute name="meta" value="/WEB-INF/tiles/${options[my_opts]}/meta.jsp

    followed by a put-list-attribute name="my_opts" within the same definitions element.

    Anyone get this working?

    Thanks in advance

  6. btw you can simply use

    instead of UrlBasedViewResolver

    In fact this is the same implementation as you defined in xml but looks more clear

  7. I mean you can use


  8. I used the same eventually today. Thanks for the comment. In the fallback method, there's also a SpringApplicationContext class that no longer exists, so I simply took it out. But I'm guessing that might be the issue. I also cracked open the OptionsRenderer to see where that exception was being thrown, and it appears to be trying to get the list attribute via a class named TilesAccess. Anywho, if no one see's a way around this maybe I can find something on the tiles mail lists or post the question there too. The fallback approach looks pretty neat, but I have yet to find a complete working example using a Spring and Tiles version I'm using, which is Spring 3.2.3.RELEASE and Tiles 3.0.1 if I recall (I'm away from my work computer, so I can't see my pom.xml at the moment).

  9. Wonder if
    could replace SpringApplicationContext because it was deleted in Nov 2012 (See
    I'm too tired to think anymore....Time to watch some A-Team reruns...

  10. Hi Dhruv, thanks for your post. Do you know how to be able using tiles to reload only the body everytime a option of the menu is clicked, leaving the header and menu static?

    Thanks in advance

  11. Dhruv, this was very helpful, and it worked the first time. :-)

    Thanks for posting it.

  12. The servlet-context.xml stuff is out of date.

    It's correct on the original blog post

    Hopefully you find that the information you were missing originally on that article (that lead you to write this entry) has since been addressed.

    1. The article link was correct, but the information on tiles3 to spring-3.2 integration is on the second page:

  13. why dont you put the source so atleast one can see and debug himself.

  14. i deleted the last one, because it removed the code,.. here again :D


    this worked awesome for me !!!!!!!


    i did some changes:
    1-I only needed the body, the header was on common too in my case.
    2- i added a third parameter in the tiles config */*/*:

    < definition name="*/*/*" extends="base" >
    < put-attribute name="body" value="/WEB-INF/views/{2}/{3}.jsp" / >
    < / definition >

    So i can use many jsp files in the same folder =D

    then the controller would return the jsp name too in this way:

    return new ModelAndView("/dreamer/registered");//for tiles
    >>"registered" is the name of the jsp =D (registered.jsp) under the folder "dreamer"

    4- i change the path of jsp files, where i had them at first ("views") this was the part that was removed from the servlet context... so i re-added it from the tiles.xml (i only change the path :D )

    ... /WEB-INF/tiles/template.jsp to /WEB-INF/views/template.jsp

    in this way (we can notice the header.jsp here):

    < definition name="base" template="/WEB-INF/views/template.jsp" >
    < put-attribute name="meta" value="/WEB-INF/views/common/meta.jsp" / >
    < put-attribute name="footer" value="/WEB-INF/views/common/footer.jsp" / >
    < put-attribute name="header" value="/WEB-INF/views/common/header.jsp" / >
    < / definition >


  15. Excellent post!!! Java is most popular and efficient programming language available in the market today. It helps developers to create stunning desktop/web applications loaded with stunning functionalities. JAVA J2EE Training in Chennai | Best JAVA Training institute in Chennai

  16. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing...
    ccna training in Chennai|ccna courses in Chennai|ccna training center in Chennai

  17. Cloud has beome the common word that is being used by most of the professional these day. The reason for relying on this technology is security. Your content too lecture the same. Thanks for sharing this worth able information in here. Keep blogging article like this.

    Hadoop Training Chennai | Big Data Training in Chennai
    | Best Hadoop Training in Chennai