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

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/

gradle-spring-output

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

4 Comments

  1. Ashay Thorat January 22, 2015
  2. Dinesh Rajput January 22, 2015
  3. karna January 23, 2015
  4. Asheesh January 19, 2019