Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Spring MVC Tiles


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

Advantage of Tiles support in Spring MVC

  1. Reusability: We can reuse a single component in multiple pages like header and footer components.
  2. Centralized control: We can control the layout of the page by a single template page only.
  3. 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 adapt new technologies such as bootstrap, jquery etc.

Spring MVC Tiles Example

Let’s see a simple spring mvc tiles example.

Required Jar files

To run this example, you need to load:

  • Spring Core jar files
  • Spring Web jar files
  • Tiles jar files

download required jar file

You need to put these jar files inside the WEB-INF/lib directory.


Directory Structure

Let’s see the files of spring tiles example in eclipse.

spring-tiles-structure

Index Page

index.jsp

  1. <a href=“hello.html”>Hello Spring</a> |
  2. <a href=“contact.html”>Contact</a>

Controller classes

HelloWorldController.java

  1. package com.Kreationnext.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.servlet.ModelAndView;
  5. @Controller
  6. public class HelloWorldController {
  7.     @RequestMapping(“/hello”)
  8.     public ModelAndView helloWorld() {
  9.         String message = “Hello World, Spring MVC @ Kreationnext”;
  10.         return new ModelAndView(“hello”“message”, message);
  11.     }
  12. }

ContactController.java

  1. package com.Kreationnext.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.validation.BindingResult;
  4. import org.springframework.web.bind.annotation.ModelAttribute;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.SessionAttributes;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import com.Kreationnext.form.Contact;
  10. @Controller
  11. @SessionAttributes
  12. public class ContactController {
  13.     @RequestMapping(value = “/addContact”, method = RequestMethod.POST)
  14.     public String addContact(@ModelAttribute(“contact”) Contact contact, BindingResult result) {
  15.         //write the code here to add contact
  16.         return “redirect:contact.html”;
  17.     }
  18.     @RequestMapping(“/contact”)
  19.     public ModelAndView showContacts() {
  20.         return new ModelAndView(“contact”“command”new Contact());
  21.     }
  22. }

Form class

Contact.java

  1. package com.Kreationnext.form;
  2. public class Contact {
  3.     private String firstname;
  4.     private String lastname;
  5.     private String email;
  6.     private String telephone;
  7.     //getters and setters
  8. }

web.xml

In this xml file, we are specifying the servlet class DispatcherServlet that acts as the front controller in Spring Web MVC. All the incoming request for the html file will be forwarded to the DispatcherServlet.

web.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app version=“2.5”
  3.     xmlns=“http://java.sun.com/xml/ns/javaee”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee 
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
  7.  <servlet>
  8.     <servlet-name>spring</servlet-name>
  9.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10.     <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13.     <servlet-name>spring</servlet-name>
  14.     <url-pattern>*.html</url-pattern>
  15. </servlet-mapping>
  16. </web-app>

spring-servlet.xml file

This is the important configuration file where we need to specify the ViewResolver and View components.

The context:component-scan element defines the base-package where DispatcherServlet will search the controller class.

Here, the UrlBasedViewResolver class is used for the ViewResolver.

The prefix+string returned by controller+suffix page will be invoked for the view component.

This xml file should be located inside the WEB-INF directory.

spring-servlet.xml

  1.  <beans xmlns=“http://www.springframework.org/schema/beans”
  2.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  3.     xmlns:p=“http://www.springframework.org/schema/p”
  4.     xmlns:context=“http://www.springframework.org/schema/context”
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  6.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7.         http://www.springframework.org/schema/context
  8.         http://www.springframework.org/schema/context/spring-context-3.0.xsd”>
  9.     <context:component-scan
  10.         base-package=“com.Kreationnext.controller” />
  11.     <bean id=“viewResolver”
  12.         class=“org.springframework.web.servlet.view.UrlBasedViewResolver”>
  13.         <property name=“viewClass”>
  14.             <value>
  15.                 org.springframework.web.servlet.view.tiles2.TilesView
  16.             </value>
  17.         </property>
  18.     </bean>
  19.     <bean id=“tilesConfigurer”
  20.         class=“org.springframework.web.servlet.view.tiles2.TilesConfigurer”>
  21.         <property name=“definitions”>
  22.             <list>
  23.                 <value>/WEB-INF/tiles.xml</value>
  24.             </list>
  25.         </property>
  26.     </bean>
  27. </beans>

tiles.xml file

tiles.xml

  1. <?xml version=“1.0” encoding=“UTF-8” ?>
  2. <!DOCTYPE tiles-definitions PUBLIC
  3.        “-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN”
  4.        “http://tiles.apache.org/dtds/tiles-config_2_0.dtd”>
  5. <tiles-definitions>
  6.     <definition name=“base.definition”
  7.         template=“/WEB-INF/jsp/layout.jsp”>
  8.         <put-attribute name=“title” value=“” />
  9.         <put-attribute name=“header” value=“/WEB-INF/jsp/header.jsp” />
  10.         <put-attribute name=“menu” value=“/WEB-INF/jsp/menu.jsp” />
  11.         <put-attribute name=“body” value=“” />
  12.         <put-attribute name=“footer” value=“/WEB-INF/jsp/footer.jsp” />
  13.     </definition>
  14.     <definition name=“contact” extends=“base.definition”>
  15.         <put-attribute name=“title” value=“Contact Manager” />
  16.         <put-attribute name=“body” value=“/WEB-INF/jsp/contact.jsp” />
  17.     </definition>
  18.     <definition name=“hello” extends=“base.definition”>
  19.         <put-attribute name=“title” value=“Hello Spring MVC” />
  20.         <put-attribute name=“body” value=“/WEB-INF/jsp/hello.jsp” />
  21.     </definition>
  22. </tiles-definitions>

View Components

hello.jsp

  1. <html>
  2. <head>
  3.     <title>Spring MVC Example</title>
  4. </head>
  5. <body>
  6. <h1>Welcome to Spring MVC</h1>
  7.     <p>Message is: ${message}</p>
  8. </body>
  9. </html>

contact.jsp

  1. <%@taglib uri=“http://www.springframework.org/tags/form” prefix=“form”%>
  2. <html>
  3. <head>
  4.     <title>Spring Tiles Contact Form</title>
  5. </head>
  6. <body>
  7. <h2>Contact Manager</h2>
  8. <form:form method=“post” action=“addContact.html”>
  9.     <table>
  10.     <tr>
  11.         <td><form:label path=“firstname”>First Name</form:label></td>
  12.         <td><form:input path=“firstname” /></td>
  13.     </tr>
  14.     <tr>
  15.         <td><form:label path=“lastname”>Last Name</form:label></td>
  16.         <td><form:input path=“lastname” /></td>
  17.     </tr>
  18.     <tr>
  19.         <td><form:label path=“lastname”>Email</form:label></td>
  20.         <td><form:input path=“email” /></td>
  21.     </tr>
  22.     <tr>
  23.         <td><form:label path=“lastname”>Telephone</form:label></td>
  24.         <td><form:input path=“telephone” /></td>
  25.     </tr>
  26.     <tr>
  27.         <td colspan=“2”>
  28.             <input type=“submit” value=“Add Contact”/>
  29.         </td>
  30.     </tr>
  31. </table>
  32. </form:form>
  33. </body>
  34. </html>

header.jsp

  1. <h2>Header</h2>
  2. <hr/>

footer.jsp

  1. <hr/>
  2. <p>Copyright  20102014 Kreationnext.com.</p>

menu.jsp

  1. <p>Menu 1</p>
  2. <p>Menu 2</p>

layout.jsp

  1. <%@ taglib uri=“http://tiles.apache.org/tags-tiles” prefix=“tiles”%>
  2. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
  3. “http://www.w3.org/TR/html4/loose.dtd”>
  4. <html>
  5. <head>
  6. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
  7. <title><tiles:insertAttribute name=“title” ignore=“true” /></title>
  8. </head>
  9. <body>
  10.         <div><tiles:insertAttribute name=“header” /></div>
  11.         <div style=“float:left;padding:10px;width:15%;”><tiles:insertAttribute name=“menu” /></div>
  12.         <div style=“float:left;padding:10px;width:80%;border-left:1px solid pink;”>
  13.         <tiles:insertAttribute name=“body” /></div>
  14.         <div style=“clear:both”><tiles:insertAttribute name=“footer” /></div>
  15. </body>
  16. </html>
download this example (developed using Eclipse)
download the required jar files

Output

spring-tiles1

spring-tiles2

spring-tiles3

Comments

comments