Gradle Spring MVC Web Project Example

In this tutorial we explain the spring mvc project and it is managed by Gradle. For this Gradle Spring MVC Web Project Example required following technologies.

  • Spring 4.0.6.RELEASE
  • STS
Gradle Tutorial

Step 1. Add Gradle plugin to STS IDE

Step 2. Create Project Structure for Dynamic Web Project As following.

Gradle Spring MVC

Step 3. Gradle Build File

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'

repositories {
dependencies {
   providedCompile 'javax.servlet:servlet-api:2.5'
   compile 'org.springframework:spring-webmvc:4.0.0.RELEASE'
   compile 'org.thymeleaf:thymeleaf-spring4:2.1.4.RELEASE'
   compile 'org.thymeleaf:thymeleaf:2.1.4.RELEASE'
   runtime 'javax.servlet:jstl:1.1.2'

Step 4. Go to the project folder, same level with build.gradle, issue the following command :

$ gradle cleanEclipse eclipse

Now, you can import the project into STS IDE.


Step 4. Right click on the project and goto the configure and click convert gradle project.


Step 5. Spring MVC Files Code

package com.doj.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;

public class HomeController {
 @RequestMapping(value="/", method = RequestMethod.GET)
 public String printWelcome(ModelMap model) {
  model.addAttribute("message", "Spring 4 MVC Hello Gradle World!!! This is a Thymeleaf template ");
  return "home";


 <h1>Message : ${message}</h1> 


<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns=""  
     <context:component-scan base-package="com.doj.controller" />  
     <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix">  
        <property name="suffix">  


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">

Step 6: Run It
Start and deploy above web project.



Step 7. Create WAR File
Clean and build a WAR file with the following command :

$ gradle clean build
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE


Total time: 4.012 secs

The generated WAR file is located at the buildlibs folder.


Download Source Code

3 thoughts on “Gradle Spring MVC Web Project Example”

  1. JSP is long gone anyways and not being actively developed. JEE uses JSF as view technology now.

    I tried Thymeleaf on couple of pet projects, and it amazed me with the simplicity and modularity. You can always write your own dialects to implement custom tags. It fits very well with spring ecosystem.

    On the other hand, it's still not complete in my openion specially lack of out of the box decorating support.

    Better to avoid Thymeleaf with AngularJS reason being it's not required.

Comments are closed.