Spring MVC Tiles (original) (raw)

Last Updated : 23 Jul, 2025

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:

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 `

4.0.0 com.javatpoint SpringMVCTiles war 0.0.1-SNAPSHOT SpringMVCTiles Maven Webapp http://maven.apache.org junit junit 3.8.1 test

<!-- 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>
org.apache.tiles tiles-jsp 3.0.5
<!-- 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>
SpringMVCTiles

`

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 `

SpringTiles index.jsp spring org.springframework.web.servlet.DispatcherServlet 1 spring *.html

`

Step 5. Create the requested page

index.jsp

HTML `

Hello geeksforgeeks | Contact us page

`

Step 6. Create the other view components

hello.jsp

HTML `

Spring MVC Example

Welcome to GeeksForGeeks

`

contact.jsp

HTML `

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

Spring Tiles Contact Form

GFG Contact Manager

<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>

`

header.jsp

HTML `

GEEEKSFORGEEKS


`

footer.jsp

HTML `


Thank you for visiting this site

`

menu.jsp

HTML `

Menu 1

Menu 2

`

layout.jsp

HTML `

<%@ taglib uri="https://tiles.apache.org/tags-tiles" prefix="tiles"%>

<tiles:insertAttribute name="title" ignore="true" />

`

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