Sunday, September 15, 2019

Spring MVC Tiles Example

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.

Directory Structure

Let's see the files of spring tiles example in eclipse.
Spring MVC Tiles Example

Spring MVC Tiles Example

1. Add dependencies to pom.xml file.

pom.xml
  1.  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
  2. <dependency>  
  3.     <groupId>org.springframework</groupId>  
  4.     <artifactId>spring-webmvc</artifactId>  
  5.     <version>5.1.1.RELEASE</version>  
  6. </dependency>  
  7.   <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
  8. <dependency>    
  9.     <groupId>javax.servlet</groupId>    
  10.     <artifactId>servlet-api</artifactId>    
  11.     <version>3.0-alpha-1</version>    
  12. </dependency>  
  13.     <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->  
  14. <dependency>  
  15.     <groupId>javax.servlet</groupId>  
  16.     <artifactId>jstl</artifactId>  
  17.     <version>1.2</version>  
  18. </dependency>  
  19. <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->  
  20. <dependency>  
  21.     <groupId>org.apache.tomcat</groupId>  
  22.     <artifactId>tomcat-jasper</artifactId>  
  23.     <version>9.0.12</version>  
  24. </dependency>  
  25.  <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->  
  26. <dependency>  
  27.     <groupId>org.apache.tiles</groupId>  
  28.     <artifactId>tiles-jsp</artifactId>  
  29.     <version>3.0.5</version>  
  30. </dependency>  
  31.       
  32.     <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-servlet -->  
  33. <dependency>  
  34.     <groupId>org.apache.tiles</groupId>  
  35.     <artifactId>tiles-servlet</artifactId>  
  36.     <version>3.0.5</version>  
  37. </dependency>  
  38.     <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-core -->  
  39. <dependency>  
  40.     <groupId>org.apache.tiles</groupId>  
  41.     <artifactId>tiles-core</artifactId>  
  42.     <version>3.0.5</version>  
  43. </dependency>  
  44.    <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-el -->  
  45. <dependency>  
  46.     <groupId>org.apache.tiles</groupId>  
  47.     <artifactId>tiles-el</artifactId>  
  48.     <version>3.0.5</version>  
  49. </dependency>  

2. Create the bean class

Contact.java
  1. package com.javatpoint.form;  
  2. public class Contact {  
  3.     private String firstname;  
  4.     private String lastname;  
  5.     private String email;  
  6.     private String telephone;  
  7.       
  8.     public String getEmail() {  
  9.         return email;  
  10.     }  
  11.     public String getTelephone() {  
  12.         return telephone;  
  13.     }  
  14.     public void setEmail(String email) {  
  15.         this.email = email;  
  16.     }  
  17.     public void setTelephone(String telephone) {  
  18.         this.telephone = telephone;  
  19.     }  
  20.     public String getFirstname() {  
  21.         return firstname;  
  22.     }  
  23.     public String getLastname() {  
  24.         return lastname;  
  25.     }  
  26.     public void setFirstname(String firstname) {  
  27.         this.firstname = firstname;  
  28.     }  
  29.     public void setLastname(String lastname) {  
  30.         this.lastname = lastname;  
  31.     }  
  32.       
  33. }  

3. Create the controller class

HelloWorldController.java
  1. package com.javatpoint.controller;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.ui.Model;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. @Controller  
  6. public class HelloWorldController {  
  7.     @RequestMapping("/hello")  
  8.     public String helloWorld(Model m) {  
  9.         String message = "Hello World, Spring MVC @ Javatpoint";  
  10.         m.addAttribute("message", message);  
  11.         return "hello";   
  12.     }  
  13. }  
ContactController.java
  1. package com.javatpoint.controller;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.ui.Model;  
  4. import org.springframework.validation.BindingResult;  
  5. import org.springframework.web.bind.annotation.ModelAttribute;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestMethod;  
  8. import org.springframework.web.bind.annotation.SessionAttributes;  
  9. import com.javatpoint.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.       
  19.     @RequestMapping("/contact")  
  20.     public String showContacts(Model m) {  
  21.         m.addAttribute("command"new Contact());  
  22.         return "contact";  
  23.     }  
  24. }  

4. Provide the entry of controller in the web.xml file

web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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">  
  3.   <display-name>SpringTiles</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.jsp</welcome-file>  
  6.   </welcome-file-list>  
  7.   <servlet>  
  8.     <servlet-name>spring</servlet-name>  
  9.     <servlet-class>  
  10.             org.springframework.web.servlet.DispatcherServlet  
  11.         </servlet-class>  
  12.     <load-on-startup>1</load-on-startup>  
  13.   </servlet>  
  14.   <servlet-mapping>  
  15.     <servlet-name>spring</servlet-name>  
  16.     <url-pattern>*.html</url-pattern>  
  17.   </servlet-mapping>  
  18. </web-app>  

5. Define the bean in the xml file

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.   
  10. <context:annotation-config />  
  11. <context:component-scan base-package="com.javatpoint.controller" />  
  12. <bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>  
  13. <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">  
  14. <property name="definitions">  
  15. <list>  
  16. <value>/WEB-INF/tiles.xml</value>  
  17. </list>  
  18. </property>  
  19. </bean>  
  20. </beans>  

6. Provide the 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.   
  19.     <definition name="hello" extends="base.definition">  
  20.         <put-attribute name="title" value="Hello Spring MVC" />  
  21.         <put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" />  
  22.     </definition>  
  23.   
  24. </tiles-definitions>  

7. Create the requested page

index.jsp
  1. <a href="hello.html">Hello Spring</a> |   
  2. <a href="contact.html">Contact</a>  

8. Create the other 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.     
  10.     <table>    
  11.     <tr>    
  12.         <td><form:label path="firstname">First Name</form:label></td>    
  13.         <td><form:input path="firstname" /></td>     
  14.     </tr>    
  15.     <tr>    
  16.         <td><form:label path="lastname">Last Name</form:label></td>    
  17.         <td><form:input path="lastname" /></td>    
  18.     </tr>    
  19.     <tr>    
  20.         <td><form:label path="lastname">Email</form:label></td>    
  21.         <td><form:input path="email" /></td>    
  22.     </tr>    
  23.     <tr>    
  24.         <td><form:label path="lastname">Telephone</form:label></td>    
  25.         <td><form:input path="telephone" /></td>    
  26.     </tr>    
  27.     <tr>    
  28.         <td colspan="2">    
  29.             <input type="submit" value="Add Contact"/>    
  30.         </td>    
  31.     </tr>    
  32. </table>      
  33.         
  34. </form:form>    
  35. </body>    
  36. </html>  
header.jsp
  1. <h2>Header</h2>    
  2. <hr/>    
footer.jsp
  1. <hr/>    
  2. <p>Copyright  2010-2014 javatpoint.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.     
  16. </body>    
  17. </html>    
Output:
Spring MVC Tiles Example
Spring MVC Tiles Example
Spring MVC Tiles Example

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...