Spring MVC Tiles Integration

Spring Framework provides integration support with Apache tiles framework.Using tiles we can manage our web application layout easily and configurable.

Advantage of using Tiles

  • Reusability
  • Centralized control
  • Easy to maintain

Required Jar files

  • Spring Core Module Jars
  • Spring Web Module Jars
  • Tiles jar files

Here I am taking example a web application to manage header,footer,sidebar of web page using tiles.

UserController.java

    package com.startwithjava.controller;  
    import org.springframework.stereotype.Controller;  
    import org.springframework.web.bind.annotation.RequestMapping;    
 @Controller  
    public class UserController {  
        @RequestMapping("/login")  
        public String login(Model model) { 
          return "login";
        }  
       @RequestMapping("/register")  
        public String register(Model model) {  return "register"; } } 

web.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
        
     <servlet>  
        <servlet-name>spring</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
          
    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping>    
    </web-app>  

spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  
    <context:component-scan base-package="com.startwithjava.controller" />  
  
    <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>  
</beans>  

tiles.xml

In tiles.xml we are defining the layout for our views such as login and register.

    <?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" 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="login" extends="base">  
            <put-attribute name="title" value="Login" />  
            <put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />  
        </definition>  
      
        <definition name="register" extends="base">  
            <put-attribute name="title" value="User Registration" />  
            <put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" />  
        </definition>  
    </tiles-definitions>  

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>

<div><tiles:insertAttribute name="header" /></div>


<div style="float:left;padding:10px;width:15%;"></div>


<div style="float:left;padding:10px;width:80%;border-left:1px solid pink;">
<tiles:insertAttribute name="body" /></div>


<div style="clear:both"><tiles:insertAttribute name="footer" /></div>


</body>
</html>

header.jsp


<h1>Header Area</h1>

footer.jsp




<h1>Footer Area</h1>



login.jsp



<form action="">
 <input type="text" name="username" value="">
 <input type="text" name="password" value="">
</form>


register.jsp


<form action="">
 <input type="text" name="username" value="">
 <input type="text" name="password" value="">
</form>

Leave a Reply