Spring 3 MVC Tiles Plug-in

Spring 3 MVC Tiles Plug-in


Posted in : Spring Posted on : March 17, 2011 at 6:25 PM Comments : [ 0 ]

In this tutorial, you will learn integration of Tiles framework with Spring 3 MVC.

Spring 3 MVC Tiles Plug-in

In this tutorial, you will learn integration of Tiles framework with Spring 3 MVC.

A web portal have many reusable templates like header, footer, menu etc. These elements remains same in every web page to give a uniform feel and look to improve presentation of portal. But difficult part is when you need to alter these common items.

The Tiles framework solve this problem by using templatization mechanism. We create a common Header, Footer, Menu page and include this in each page. A common layout of website is defined in a central configuration file and this layout can be extended across all the web pages of the web application.

Example

In the given below example, we used Tiles 2 framework with Spring 3 MVC. We are going to create the following layout in which we add header, footer, menu etc.

Project hierarchy

Jar file used

CODES

web.xml ( /Spring3MvcTilePlugin/WebContent/WEB-INF/web.xml )

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MvcTilePlugin</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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>*.html</url-pattern>
</servlet-mapping>
</web-app>

spring-servlet.xml ( /Spring3MvcTilePlugin/WebContent/WEB-INF/spring-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: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="net.devmanuals.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>

index.jsp ( /Spring3MvcTilePlugin/WebContent/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>Spring 3 MVC Tile Plug-in</title>
</head>
<body>
<jsp:forward page="contacts.html"></jsp:forward>
</body>
</html>

tiles.xml ( /Spring3MvcTilePlugin/WebContent/WEB-INF/tiles.xml )

<?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.definition" 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="contact" extends="base.definition">
<put-attribute name="title" value="Contact Manager" />
<put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
</definition>

</tiles-definitions>

ContactController.java ( /Spring3MvcTilePlugin/src/net/devmanuals/controller/ContactController.java )

package net.devmanuals.controller;

import net.devmanuals.form.Contact;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes
public class ContactController {

@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")
Contact contact, BindingResult result) {

System.out.println("First Name:" + contact.getFirstname() + 
"Last Name:" + contact.getLastname());


return "redirect:contacts.html";
}

@RequestMapping("/contacts")
public ModelAndView showContacts() {

return new ModelAndView("contact", "command", new Contact());
}
}

HelloWorldController.java ( /Spring3MvcTilePlugin/src/net/devmanuals/controller/HelloWorldController.java )

package net.devmanuals.controller;


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

@Controller
public class HelloWorldController {

@RequestMapping("/hello")
public ModelAndView helloWorld() {

String message = "Hello World, Spring 3.0!";
System.out.println(message);
return new ModelAndView("hello", "message", message);
}

}

Contact.java ( /Spring3MvcTilePlugin/src/net/devmanuals/form/Contact.java )

package net.devmanuals.form;


public class Contact {
private String firstname;
private String lastname;
private String email;
private String telephone;


public String getEmail() {
return email;
}
public String getTelephone() {
return telephone;
}
public void setEmail(String email) {
this.email = email;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}

}

contact.jsp ( /Spring3MvcTilePlugin/WebContent/WEB-INF/jsp/contact.jsp )

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring 3 MVC Series - Contact Manager</title>
</head>
<body>
<h2>Contact Manager</h2>
<form:form method="post" action="addContact.html">

<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td> 
</tr>
<tr>
<td><form:label path="lastname">Last Name</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Email</form:label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><form:label path="lastname">Telephone</form:label></td>
<td><form:input path="telephone" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table> 

</form:form>
</body>
</html>

footer.jsp ( /Spring3MvcTilePlugin/WebContent/WEB-INF/jsp/footer.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>Spring3 MVC Tiles Plug-in</title>
</head>
<body>
Copyright &copy; Devmanuals.com
</body>
</html>

header.jsp ( /Spring3MvcTilePlugin/WebContent/WEB-INF/jsp/header.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>Spring3 MVC Tiles Plug-in</title>
</head>
<body>
<h1>Devmanuals Spring 3 MVC Tiles</h1>
</body>
</html>

Hello.jsp ( /Spring3MvcTilePlugin/WebContent/WEB-INF/jsp/hello.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>Spring3 MVC Tiles Plug-in</title>
</head>
<body>
${message}
</body>
</html>

layout.jsp ( /Spring3MvcTilePlugin/WebContent/WEB-INF/jsp/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 bgcolor="yellow">
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>
<td height="30" colspan="2"><tiles:insertAttribute name="header" />
</td>
</tr>
<tr>
<td height="250"><tiles:insertAttribute name="menu" /></td>
<td width="350"><tiles:insertAttribute name="body" /></td>
</tr>
<tr>
<td height="30" colspan="2"><tiles:insertAttribute name="footer" />
</td>
</tr>
</table>
</body>
</html>

menu.jsp ( /Spring3MvcTilePlugin/WebContent/WEB-INF/jsp/menu.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>Spring3 MVC Tiles Plug-in</title>
</head>
<body>
<p>Menu</p>
</body>
</html>

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics