Spring MVC — Tiles View Resolver | Code Factory

Donate : Link

WordPress Blog : Link

Applications… : Link

Spring Tutorial Index Page: Link

  • Spring provides integration support with apache tiles framework. So we can simply manage the layout of the Spring MVC application with the help of spring tiles support.

Advantage of Tiles support in Spring MVC

  • Reusability: We can reuse a single component in multiple pages like header and footer components.
  • Centralized control: We can control the layout of the page by a single template page only.
  • Easy to change the layout: By the help of single template page, we can change the layout of the page anytime. So your website can easily adopt new technologies such as bootstrap, jQuery, etc.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Dynamic Web Project
  • Create MVC-HelloWorld-TVR project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs (Used 5.X jars)
    - Please see below screenshot
  • Also, add these jars in WEB-INF/lib folder for runtime.
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

Note: Used Spring 5.X and Apache Tiles 3.0.5 Jars.
Apache Tiles Link: http://archive.apache.org/dist/tiles/v3.0.5/

Instead of download jars, you can also use pom file.

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-servlet -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-core -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-el -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-el</artifactId>
<version>3.0.5</version>
</dependency>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<display-name>MVC-HelloWorld-TVR</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- default -->
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloWorld</servlet-name>
<url-pattern>*.code</url-pattern>
</servlet-mapping>
</web-app>

helloWorld-servlet.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans> <!-- default handler mapping -->
<!-- if we comment below like still it will work fine
because it is default handler mapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- controller -->
<bean name="/hello.code" class="com.codeFactory.controller.HelloController" />
<!-- view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<value>/WEB-INF/tiles.xml</value>
</property>
</bean>
</beans>

tiles.xml

<!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.definition" template="layout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/header.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/footer.jsp" />
</definition>
<definition name="hello" extends="base.definition">
<put-attribute name="title" value="Tiles View Resolver Example" />
<put-attribute name="body" value="/hello.jsp" />
</definition>
</tiles-definitions>

HelloController.java

package com.codeFactory.controller;import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller { @Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
Map map = new HashMap();
map.put("resolver", "TilesViewResolver");
map.put("message", "Hello " + name);

ModelAndView modelAndView = new ModelAndView("hello", map);
return modelAndView;
}
}

index.jsp

<h2>Hello World</h2>
<form action="./hello.code">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>

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: 15px; width: 100%; border-left: 1px solid red;">
<tiles:insertAttribute name="body" />
</div>
<div style="clear: both">
<tiles:insertAttribute name="footer" />
</div>
</body>
</html>

hello.jsp

View Resolvers : ${resolver}
<br/>
${message}

header.jsp

<h2>Header Page</h2>

footer.jsp

<h2>Footer Page</h2>