Monday, June 6, 2011

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 comments:

  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

    ReplyDelete
  2. Hey you may want to add that you need WEB-INF in the tiles defs for the path to a jsp

    ReplyDelete
    Replies
    1. 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.

      Delete
    2. 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

      Delete
    3. Based on the error stacktrace, it seems log4j.jar is not in classpath.
      Add log4j jar file to classpath.

      Delete
  3. 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)

    ReplyDelete
    Replies
    1. Any findings on this? I'm facing the same

      Delete
    2. I have the same..can you please update if you had solved it?

      Delete
    3. Hi,
      I have deployed and tested the application on JBoss 5.1.0 and JBoss AS 7.1.0.FINAL servers and is working fine. This issue seems present in very initial versions of Spring 3 libraries, see https://jira.springsource.org/browse/SPR-6606.

      Let me know with which JBoss version you are facing issue.

      Thanks,
      Siva

      Delete
  4. Did you resolve this issue? I am having the same exception.

    ReplyDelete
  5. Hi Siva,
    I had to use instead of to get it worked. Is it working as it is for you

    ReplyDelete
  6. Hi,

    what is the web.xml configuration.
    what should be the startup page,
    servlet configuration.

    ReplyDelete
  7. 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

    ReplyDelete
    Replies
    1. Hi Sandeep,
      I have created a sample application as i described in the post and uploaded into my GitHub repo.
      https://github.com/sivaprasadreddy/sivalabs-blog-samples-code/tree/master/springmvc-tiles

      Please see the code and let me know if you still face any issues. You can reach me at sivaprasadreddy.k@gmail.com

      Thanks,
      Siva

      Delete
    2. Just FYI, I have tested it on Tomcat 6.0.37 and 7.0.40 and is working fine.

      Delete
  8. best article on spring mvc layout creation, easy and straight to the point. kudos to you siva.

    ReplyDelete