SpringMVC 3 + Tiles 2.2.2 Integration

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 http://tiles.apache.org/ .

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






/WEB-INF/tiles.xml





<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>



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

 



<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" />



<put-attribute name="title" value="SivaLabs : Login" />
<put-attribute name="navigation" value="" />
<put-attribute name="body" value="/jsp/login.jsp" />



<put-attribute name="title" value="SivaLabs : Welcome" />
<put-attribute name="body" value="/jsp/welcome.jsp" />



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>

</head>
<body>












<tiles:insertAttribute name="header" />


<tiles:insertAttribute name="navigation" />


<tiles:insertAttribute name="body" />


<tiles:insertAttribute name="footer" />

</body>
</html>

header.jsp


SivaLabs : My Experiments On Technology


footer.jsp


© 2011 SivaLabs All Rights Reserved

navigation.jsp

Create User

View Users

Logout

welcome.jsp


Welcome to SpringMVC+Tiles Sample Application


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.

You can download the code from https://github.com/sivaprasadreddy/sivalabs-blog-samples-code/tree/master/springmvc-tiles

17 thoughts on “SpringMVC 3 + Tiles 2.2.2 Integration”

  1. A lot of people is seeing this page, but no comments, well this is the first, good job, i am getting started with tiles and spring, thanks and hope you can continue with more experiments to increase your skills

  2. At the time of writing this article, i did put my jsps directly in WebContent/jsp folder. So I didn't need to add /WEB-INF/ prefix to jsps path.

  3. Hi,
    I am getting below error while using spring3 with tiles, i have included log4j-over-slf4j-1.6.4.jar, but still result is same

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.Log4jLoggerFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)

    Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.Log4jLoggerFactory
    at org.apache.log4j.LogManager.getLogger(LogManager.java:42)
    at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:64)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:253)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:265)
    at org.apache.tiles.startup.AbstractTilesInitializer.(AbstractTilesInitializer.java:45)
    at org.apache.tiles.startup.BasicTilesInitializer.(BasicTilesInitializer.java:37)
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer$SpringTilesInitializer.(TilesConfigurer.java:371)
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer$SpringTilesInitializer.(TilesConfigurer.java:371)
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer.createTilesInitializer(TilesConfigurer.java:352)
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer.afterPropertiesSet(TilesConfigurer.java:317)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
    … 33 more

    Thanks in advance

  4. Hi, i need your help…

    I try to integrate Spring 3.1.1 with tiles 2.2.2, i have this libraries :

    commons-beanutils-1.8.3.jar
    commons-beanutils-bean-collections-1.8.3.jar
    commons-beanutils-core-1.8.3.jar
    commons-digester-2.1.jar
    jcl-over-slf4j-1.6.3.jar
    slf4j-api-1.6.3.jar
    slf4j-log4j12-1.6.3.jar
    tiles-api-2.2.2.jar
    tiles-core-2.2.2.jar
    tiles-jsp-2.2.2.jar
    tiles-servlet-2.2.2.jar

    but jboss return this error in the deploy

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined in ServletContext resource [/WEB-INF/iquaback-servlet.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: class org.apache.tiles.servlet.context.ServletTilesRequestContextFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

  5. Hi Siva,

    Sorry to say, it is not working for me. I am sure that it is related to setup issue (jars and all), for expert like you, should not be a problem but for me it's giving a very hard time 🙂

    I have configured following as POM.XML. If you have time, can you please correct me?

    junit
    junit
    3.8.1
    test

    org.apache.tiles
    tiles-template
    2.2.1

    org.apache.tiles
    tiles-servlet
    2.2.1

    org.apache.tiles
    tiles-jsp
    2.2.1

    org.springframework
    spring-webmvc
    3.1.2.RELEASE

    org.springframework
    spring-web
    3.1.2.RELEASE

    jstl
    jstl
    1.2

    org.slf4j
    slf4j-log4j12
    1.4.2

Leave a Reply

Your email address will not be published. Required fields are marked *