Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Spring MVC - Tiles
Next article icon

Spring MVC - Tiles

Last Updated : 02 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Spring provides functionality for integrating with the Apache Tiles framework. With the help of spring tiles support, we can easily manage the layout of the Spring MVC application.

Benefits of Spring MVC's Tiles support:

The following are some of the primary benefits of Spring MVC Tiles 3 Integration:

  • Reusability: A single component, such as the header and footer, can be reused across several pages.
  • Centralized control: We can control the layout of the page from a single template page.
  • Alter the layout easily: We can change the layout of the page at any time using a single template page. As a result, new technologies such as bootstrap, jQuery, and others can be readily integrated into your website.

Example Project

Project structure:

Project structure

Let's create a working example with the Eclipse IDE, which will include the following steps:

  1. Create a project called SpringEx with the package com.geeksforgeeks. This should be in your newly formed project's src folder.
  2. Using the Add External JARs options, add the Spring Libraries that are required.
  3. With index.jsp, you may create an index page.
  4. Create HelloWorldController.java, ContactController.java, and Contact.java under the above-made package.
  5. Under the src folder, create the web.xml, tile.xml, and spring-servlet.xml configuration files.
  6. Create all of the View components' code.
  7. Finally, write code for all Java files and the Bean configuration file, then run the application as directed.

Step 1: Update the pom.xml file to include dependencies

pom.xml

You can download the required dependencies from URLs given in the comments of the program.

XML
<project xmlns="http://maven.apache.org/POM/4.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0                       http://maven.apache.org/maven-v4_0_0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.javatpoint</groupId>   <artifactId>SpringMVCTiles</artifactId>   <packaging>war</packaging>   <version>0.0.1-SNAPSHOT</version>   <name>SpringMVCTiles Maven Webapp</name>   <url>http://maven.apache.org</url>   <dependencies>     <dependency>         <groupId>junit</groupId>         <artifactId>junit</artifactId>         <version>3.8.1</version>         <scope>test</scope>     </dependency>          <!-- 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>        </dependencies>   <build>     <finalName>SpringMVCTiles</finalName>   </build> </project> 

Step 2: Create the bean class

Contact.java

Java
package com.geeksforgeeks.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;     } } 

Step 3. Create the controller class

HelloWorld.java

Java
package com.geeksforgeeks.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;  @Controller public class HelloWorld  {     @RequestMapping("/hello")     public String helloWorld(Model m)      {         String message = "Hello geeksforgeeks";         m.addAttribute("message", message);         return "hello";      } } 

ContactController.java

Java
package com.geeksforgeeks.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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 com.geeksforgeeks.form.Contact;  @Controller @SessionAttributes public class ContactController  {     @RequestMapping(value = "/addContact", method = RequestMethod.POST)     public String addContact(@ModelAttribute("contact")    Contact contact, BindingResult result)      {         // write the code here to add contact         return "redirect:contact.html";     }          @RequestMapping("/contact")     public String showContacts(Model m)      {         m.addAttribute("command", new Contact());         return "contact";     } } 

Step 4. Provide a controller entry in the web.xml file

web.xml

XML
<?xml version="1.0" encoding="UTF-8"?> <web-app>   <display-name>SpringTiles</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> 

Step 5. Create the requested page

index.jsp

HTML
<a href="hello.html">Hello geeksforgeeks </a>  |  <a href="contact.html">Contact us page</a> 

Step 6. Create the other view components

hello.jsp

HTML
<html> <head>     <title>Spring MVC Example</title> </head> <body>     <h1>Welcome to GeeksForGeeks</h1> </body> </html> 

contact.jsp

HTML
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head>     <title>Spring Tiles Contact Form</title> </head> <body> <center> <h1> GFG Contact Manager</h1> <form:form method="post" action="addContact.html">      <table>     <tr>         <td><form:label path="firstname">Enter your firstname</form:label></td>         <td><form:input path="firstname" /></td>      </tr>     <tr>         <td><form:label path="lastname">Enter your lastname</form:label></td>         <td><form:input path="lastname" /></td>     </tr>     <tr>         <td><form:label path="lastname">Enter your email</form:label></td>         <td><form:input path="email" /></td>     </tr>     <tr>         <td><form:label path="lastname">Enter your mobile.no</form:label></td>         <td><form:input path="telephone" /></td>     </tr>     <tr>         <td colspan="2">             <input type="submit" value="Submit"/>         </td>     </tr> </table>          </form:form> </center> </body> </html> 

header.jsp

HTML
<h2>GEEEKSFORGEEKS</h2> <hr/> 

footer.jsp

HTML
<hr/>  <p>Thank you for visiting this site</p> 

menu.jsp

HTML
<p>Menu 1</p>   <p>Menu 2</p> 

layout.jsp

HTML
<%@ 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:10px;width:15%;"><tiles:insertAttribute name="menu" /></div>         <div style="float:left;padding:10px;width:80%;border-left:3px solid black;">         <tiles:insertAttribute name="body" /></div>         <div style="clear:both"><tiles:insertAttribute name="footer" /></div>  </body> </html> 

Output:

After clicking the "Hello geeksforgeeks" link following page will be shown

Output

And after clicking on contact us this page will be shown

Output

Next Article
Spring MVC - Tiles

S

sanketnagare
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Java-Spring
  • Java-Spring-MVC
Practice Tags :
  • Java

Similar Reads

    Data Transfer Object (DTO) in Spring MVC with Example
    In Spring Framework, Data Transfer Object (DTO) is an object that carries data between processes. When you're working with a remote interface, each call is expensive. As a result, you need to reduce the number of calls. The solution is to create a Data Transfer Object that can hold all the data for
    7 min read
    Spring MVC - Capture and Display the Data from Registration Form
    This article is the continuation of this article Spring MVC - Create Registration Form using Form Tag Library, where we have successfully created a registration form using the Form Tag Library. Here in this article, we are going to explain how can we capture the data that are entered by the user and
    3 min read
    Spring MVC - Create Registration Form using Form Tag Library
    Spring Framework provides spring’s form tag library for JSP views in Spring’s Web MVC framework. In Spring Framework, we use Java Server Pages(JSP) as a view component to interact with the user. From version 2.0, Spring Framework provides a comprehensive set of data binding-aware tags. These tags ar
    7 min read
    Data Binding in Spring MVC with Example
    Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
    8 min read
    Spring MVC - Pagination with Example
    We will be explaining how we can implement pagination in Spring MVC Application. This is required when we need to show a lot of data on pages. Suppose in an e-commerce site we have a lot of products but we can't show all of those on a single page, so we will show only 20 products on each page. This
    3 min read
    Spring MVC Integration with MySQL
    Spring MVC is one of the most popular Java frameworks for building scalable web applications. When combined with MySQL, it provides a robust solution for developing data-driven applications. This article will guide you through integrating Spring MVC with MySQL, covering database setup, project confi
    7 min read
    OpenSource REST API URL and Retrieving Data From it By Using Spring MVC
    In this internet era, a lot of helper services are available in the form of REST API and mostly REST API provides us the details in the form of JSON/XML. We can use them in our applications and render the data as we like. Here is the list of a few opensource REST API URLs: API NameDescriptionURLCoin
    6 min read
    How to Resolve WEB xml is missing and failOnMissingWebXml is set to true in Eclipse/STS?
    Eclipse/STS IDE is generally used to develop Spring applications and what happens is whenever we are creating a simple Maven project and if the web.xml is missing or you have deleted that file then you may encounter this problem inside the pom.xml file corresponding to which do refer to the below im
    2 min read
    Spring MVC Application Without web.xml File
    Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. Here we will be creating and running Your First Spring MVC Application,
    4 min read
    Spring - MVC Listbox
    Spring Web MVC framework to demonstrate how to utilize Listbox in forms. Let's start by setting up an Eclipse IDE and then following the steps to create a Dynamic Form-based Web Application utilizing the Spring Web Framework. The items are listed in the Spring MVC form Listbox. This tag creates a se
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences