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.
  • gradle-2.2.1-all.zip
  • Spring 4.0.6.RELEASE
  • STS

Step 1. Add Gradle plugin to STS IDE

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

Step 3. Gradle Build File
Build.gradle

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

repositories {
   mavenCentral()
}
 
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.

or

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

Step 5. Spring MVC Files Code

HomeController.java

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;

@Controller
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";
 
 }
}

home.jsp

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

mygradleapp-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:context="http://www.springframework.org/schema/context"  
     xmlns:p="http://www.springframework.org/schema/p"    
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
      
     <context:component-scan base-package="com.doj.controller" />  
             
     <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix">  
          <value>/WEB-INF/view/</value>  
        </property>  
        <property name="suffix">  
          <value>.jsp</value>  
        </property>  
      </bean>   
</beans>  

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>mygradleapp</display-name>
  <servlet>
    <servlet-name>mygradleapp</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mygradleapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

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

http://localhost:8080/mygradleapp/

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

$ gradle clean build
:clean
:compileJava
:processResources
:classes
:war
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build

BUILD SUCCESSFUL

Total time: 4.012 secs

The generated WAR file is located at the buildlibs folder.

${Project}buildlibsmygradleapp.war

Download Source Code

mygradleapp.zip

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago