I configured my Spring based application (4.1.7) to use Tiles 3.0.5 but it is not working. It does not show header, footer in baseLayout. It just shows index.jsp page
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.learning.springmvc</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Learning</name>
<properties>
<org.springframework.version>4.1.7.RELEASE</org.springframework.version>
<apache.tiles>3.0.5</apache.tiles>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>${apache.tiles}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
tile-definition.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software
Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="baseLayout" template="/WEB-INF/tiles/template/baseLayout.jsp">
<put-attribute name="title" value="Layout Title"/>
<put-attribute name="heading" value=""/>
<put-attribute name="tagline" value=""/>
<put-attribute name="header" value="/WEB-INF/tiles/template/header.jsp"/>
<put-attribute name="content" value=""/>
<put-attribute name="footer" value="/WEB-INF/tiles/template/footer.jsp"/>
</definition>
<definition name="home" extends="baseLayout">
<put-attribute name="title" value="Home" />
<put-attribute name="heading" value="Home" />
<put-attribute name="tagline" value="Home" />
<put-attribute name="content" value="/WEB-INF/views/index.jsp" />
</definition>
</tiles-definitions>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.learning.controller"/>
<!-- define resource folder location -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- For tiles -->
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="-2" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/definitions/tile-definition.xml</value>
</list>
</property>
</bean>
</beans>
Controller
package com.learning.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class WelcomeController {
@RequestMapping(value="/", method=RequestMethod.GET )
public String index(ModelMap model)
{
model.addAttribute("message", "Welcome to Home Page");
return "home";
}
@RequestMapping(value="/welcome", method=RequestMethod.GET )
public String welcome(ModelMap model)
{
model.addAttribute("message", "Welcome to Home Page");
return "welcome";
}
}
baseLayout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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:getAsString name="title"/></title>
<jsp:include page="baseCss.jsp" />
</head>
<body>
<!--header and navigation start-->
<jsp:include page="header.jsp" />
<!--header and navigation end-->
<!--main content start-->
<tiles:insertAttribute name="content"/>
<!--main content end-->
<!--footer start-->
<jsp:include page="footer.jsp" />
<!--footer end-->
<jsp:include page="baseJS.jsp" />
</body>
</html>
index.jsp
<div class="container_12">
This is index page
</div
The reason you are not seeing header and footer is because you are using
<jsp:include page="footer.jsp" />
Instead you should use
<tiles:insertAttribute name="footer" />
Please follow the guide below if the above does not fix your issue immediately
Remove
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
Change this
<!-- For tiles -->
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="-2" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/definitions/tile-definition.xml</value>
</list>
</property>
</bean>
With This
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/definitions/tile-definition.xml</value>
</list>
</property>
</bean>
Change baseLayout.jsp with the folling
<title>
<tiles:insertAttribute name="title"></tiles:insertAttribute>
</title>
<body>
<header class="header">
<tiles:insertAttribute name="header" />
</header>
<div class="content">
<tiles:insertAttribute name="content" />
</div>
<footer class="footer">
<tiles:insertAttribute name="footer" />
</footer>
</body>
POM
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.5</version>
</dependency>