Search code examples
springspring-mvcapache-tiles

Spring MVC Apache tiles: footer is missing from output


I am now in Spring mvc trying to learn apche tiles...but its not working....don't know why?

****servlet-context.xml** file**

<?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.0.xsd
 http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.0.xsd">




<mvc:annotation-driven />



<bean   
  class="org.springframework.web.servlet.view.
     InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<context:component-scan base-package="com.mymvc.myapp" />

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/defs/general.xml</value>
        </list>
    </property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles3.TilesView" />
< /bean>


</beans>

Tiles dependicies in pom.xml

<dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${apache.tiles}</version>
</dependency>

<dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${apache.tiles}</version>
</dependency>

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.6</version>
</dependency>

general.xml

  <?xml version="1.0" encoding="ISO-8859-1" ?>
  <!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="common" template="/WEB-INF/layout/classic.jsp">
     <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
 </definition>

  <definition name="index" extends="common">
    <put-attribute name="title" value="Java Blog Aggregator" />
    <put-attribute name="body" value="/WEB-INF/views/index.jsp" />
 </definition>
 </tiles-definitions>

classic.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title><tiles:getAsString name="title"/></title>
 </head>
  <body>
     <tiles:insertAttribute name="body" />
     <br>
     <br>
     <center>
            <tiles:insertAttribute name="footer" />
     </center>
  </body>
 </html>

footer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

   &copy;IShmam Shahriar

IndexController.java

package com.mymvc.myapp;

 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;

 @Controller
 public class IndexController {
      @RequestMapping("/index")
       public String index(){
           return "index";
       }
  }

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <!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=ISO- 
      8859-1">
      <title>Insert title here</title>
  </head>
  <body>
       <p>index page</p>
   </body>
 </html>

Solution

  • Your HTML is wrong! It has a closing <body> and <html> tag in you "main-content (index.jsp)"!

    You have your template: classic.jsp and you include index.jsp in the <tiles:insertAttribute name="body" /> placeholder. So the resulting HTML would looks like, (the complete index.jsp will be inserted at the placeholder!):

    <html>
       <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <title>...</title>
       </head>
       <body>
    <html>                            <!-- problem starts her -->
      <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO8859-1">
          <title>Insert title here</title>
      </head>
      <body>
           <p>index page</p>
       </body>                        <!-- that is the reason for not -->
     </html>                          <!-- having a footer -->
             <br>
            <br>
             <center>
                &copy;IShmam Shahriar
             </center>
         </body>
     </html>
    

    Did you noticed that you "close" the html content with </body> and </html> already before the footer! -- The solution is simple: remove all the header, html and body tags from index.jsp:

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
    
       <p>index page</p>
    

    An other point:

    Remove your InternalResourceViewResolver from the configuration. The problem is that you have two view resolvers (Internal and Url/Tiles) and it is likely that your viewname (return "index") is resolved by the Internal view resolver, but not by the Url/Tiles resolver.

    According to Spring Reference, Chapter 18.3.2 Integration Tiles, you just need:

    • org.springframework.web.servlet.view.tiles3.TilesConfigurer
    • org.springframework.web.servlet.view.UrlBasedViewResolver with TilesView configured for viewClass property (or use org.springframework.web.servlet.view.tiles3.TilesViewResolver - that is an anready configured subclass of UrlBasedViewResolver)
      • and you can have a org.springframework.web.servlet.view.ResourceBundleViewResolver

    example:

    <bean id="tilesConfigurer"
       class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
       <property name="definitions">
           <list>
               <value>/WEB-INF/defs/general.xml</value>
            </list>
        </property>
    </bean>
    
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
    </bean>