Spring MVC Tiles Example | Spring With Tiles Example Online Tutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorials, I am going to show you how to work with Spring MVC With Apache Tiles.
Spring MVC Tiles are mostly used combination. Apache Tiles is a templating framework helpful to minimize the development effort of web application (Spring, Struts) user interfaces.
Spring 4.3.4.RELEASE
Apache Tiles 3.0.7
Servlet API 3.1.0
Maven 3.6.0
Java 1.7 and
STS 3.6.4.RELEASE
pom.xml
<properties>
        <springframework.version>4.3.4.RELEASE</springframework.version>
        <apache-tiles.version>3.0.7</apache-tiles.version>
        <javax.servlet-api.version>3.1.0</javax.servlet-api.version>
        <javax.servlet.jsp-api.version>2.3.1</javax.servlet.jsp-api.version>
        <jstl.version>1.2</jstl.version>
        <java.version>1.7</java.version>

        <maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
        <maven-war-plugin.version>2.6</maven-war-plugin.version>

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- Apache Tiles -->
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>${apache-tiles.version}</version>
        </dependency>
        <!-- Servlet+JSP+JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet-api.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>${javax.servlet.jsp-api.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
    </dependencies>
Create ApplicationConfiguration file, since we are using Servlet 3.x version, there is no web.xml. The below class replaces the web.xml role.
ApplicationConfig.java
package com.onlinetutorialspoint.tiles.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.onlinetutorialspoint.tiles")
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/views/**/tiles.xml" });
        tilesConfigurer.setCheckRefresh(true);
        return tilesConfigurer;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        TilesViewResolver viewResolver = new TilesViewResolver();
        registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

}
Create ApplicationInitializer to start up the application.
ApplicationInitializer.java
package com.onlinetutorialspoint.tiles;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.onlinetutorialspoint.tiles.config.ApplicationConfig;

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { ApplicationConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}
Create a Controller.
HomeController.java
package com.onlinetutorialspoint.tiles.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class HomeController {
    @RequestMapping(value = { "/" }, method = RequestMethod.GET)
    public String homePage(ModelMap model) {
        return "home";
    }

    @RequestMapping(value = { "/admin" }, method = RequestMethod.GET)
    public String productsPage(ModelMap model) {
        return "admin";
    }

    @RequestMapping(value = { "/user" }, method = RequestMethod.GET)
    public String contactUsPage(ModelMap model) {
        return "user";
    }
}
Create necessary views:
home.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>Home</title>
</head>
<body>
    <h2>Welcome to OnlineTutorialsPoint Spring MVC Tiles Tutorials</h2>
</body>
</html>
user.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 MVC</title>
</head>
<body>
    <h2>Welcome User :)</h2>
</body>
</html>
admin.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>Apache Tiles</title>
</head>
<body>
    <h2>Welcome Admin :)</h2>
</body>
</html>
Configure the Apache tiles:
tiles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC  "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"  "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">  
 
<tiles-definitions>  
  
   <!-- Template Definition -->
   <definition name="template-def"
       template="/WEB-INF/views/tiles/layouts/defaultLayout.jsp">  
       <put-attribute name="title" value="" />  
       <put-attribute name="header" value="/WEB-INF/views/tiles/templates/header.jsp" />  
       <put-attribute name="menu" value="/WEB-INF/views/tiles/templates/menu.jsp" />  
       <put-attribute name="body" value="" />  
       <put-attribute name="footer" value="/WEB-INF/views/tiles/templates/footer.jsp" />  
   </definition>  
  
   <!-- Main Page -->
   <definition name="home" extends="template-def">  
       <put-attribute name="title" value="Welcome" />  
       <put-attribute name="body" value="/WEB-INF/views/pages/home.jsp" />  
   </definition>  
 
   <!-- User Page -->
   <definition name="user" extends="template-def">  
       <put-attribute name="title" value="User" />  
       <put-attribute name="body" value="/WEB-INF/views/pages/user.jsp" />  
   </definition>  
       
   <!-- Admin Page -->
   <definition name="admin" extends="template-def">  
       <put-attribute name="title" value="Admin" />  
       <put-attribute name="body" value="/WEB-INF/views/pages/admin.jsp" />  
   </definition>  
  
</tiles-definitions>
header.jsp
<header>
  <h1>Welcome To OnlineTutorialsPoint MVC Ttorials</h1>
</header>
footer.jsp
<footer>copyright © OnlineTutorialsPoint</footer>
menu.jsp
<nav class="nav">
    <a href="${pageContext.request.contextPath}/"></a>
    <ul id="menu">
       <li><a href="${pageContext.request.contextPath}/">Home</a></li>
       <li><a href="${pageContext.request.contextPath}/user">User</a></li>
       <li><a href="${pageContext.request.contextPath}/admin">Admin</a></li>
    </ul>
</nav>
http://localhost:8080/spring-mvc-tiles/
Happy Learning 🙂

Spring MVC Form Validation Example
Spring Hibernate Example
Spring MVC HelloWorld
Spring Boot How to change the Tomcat to Jetty Server
How to Get All Spring Beans Details Loaded in ICO
Spring Boot MVC Example Tutorials
Spring Boot Validation Login Form Example
Simple Spring Boot Example
How to Create own Spring Boot Error Page
Spring Boot In Memory Basic Authentication Security
Types of Spring Bean Scopes Example
Dependency Injection (IoC) in spring with Example
Spring Web MVC Framework Flow
Spring Boot Apache ActiveMq In Memory Example
External Apache ActiveMQ Spring Boot Example

Spring MVC Form Validation Example
Spring Hibernate Example
Spring MVC HelloWorld
Spring Boot How to change the Tomcat to Jetty Server
How to Get All Spring Beans Details Loaded in ICO
Spring Boot MVC Example Tutorials
Spring Boot Validation Login Form Example
Simple Spring Boot Example
How to Create own Spring Boot Error Page
Spring Boot In Memory Basic Authentication Security
Types of Spring Bean Scopes Example
Dependency Injection (IoC) in spring with Example
Spring Web MVC Framework Flow
Spring Boot Apache ActiveMq In Memory Example
External Apache ActiveMQ Spring Boot Example






Nikit Kumar
January 25, 2018 at 3:19 pm - Reply 

mail me this project : kumar.nikit1@gmail.com









Koushik Ghosh
October 12, 2019 at 7:44 pm - Reply 

You have not provided defaultLayout.jsp. It would kind of you if you provide the same to my mail.
Thanks,
Koushik Ghosh










Nikit Kumar
January 25, 2018 at 3:19 pm - Reply 

mail me this project : kumar.nikit1@gmail.com




mail me this project : kumar.nikit1@gmail.com





Koushik Ghosh
October 12, 2019 at 7:44 pm - Reply 

You have not provided defaultLayout.jsp. It would kind of you if you provide the same to my mail.
Thanks,
Koushik Ghosh




You have not provided defaultLayout.jsp. It would kind of you if you provide the same to my mail.