<div dir="ltr" style="text-align: justify;" trbidi="on">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.</p>
<ul style="text-align: left;">
<li><i><b>gradle-2.2.1-all.zip</b></i></li>
<li><i><b>Spring 4.0.6.RELEASE</b></i></li>
<li><i><b>STS</b></i></li>
</ul>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://50.87.249.200/~dineshon/wp-content/uploads/2015/01/gradle.png"/></div>
<p>
<b>Step 1.</b> Add Gradle plugin to STS IDE</p>
<p> <b>Step 2.</b> Create Project Structure for Dynamic Web Project As following.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://50.87.249.200/~dineshon/wp-content/uploads/2015/01/gradle-spring.png" /></div>
<p>
<b>Step 3.</b> Gradle Build File<br />
<b>Build.gradle</b></p>
<pre class="highlight" name="code">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'
}
</pre>
<div align='center' id="ads-id"></div>
<p><b>Step 4</b>. Go to the project folder, same level with build.gradle, issue the following command :</p>
<p><i>$ gradle cleanEclipse eclipse</i></p>
<p>Now, you can import the project into STS IDE.</p>
<p><b>or</b></p>
<p><b>Step 4.</b> Right click on the project and goto the configure and click convert gradle project.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://50.87.249.200/~dineshon/wp-content/uploads/2015/01/convert-gradle-project.png" /></div>
<p>
<b>Step 5</b>. Spring MVC Files Code </p>
<p><b>HomeController.java</b></p>
<pre class="highlight" name="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;

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

</pre>
<p>
<b>home.jsp</b></p>
<pre class="highlight" type="code"><;html>;
<;body>;
 <;h1>;Message : ${message}<;/h1>; 
<;/body>;
<;/html>;
</pre>
<p>
<b>mygradleapp-servlet.xml</b></p>
<pre class="highlight" name="code"><;?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>; 
</pre>
<p>
<b>web.xml</b></p>
<pre class="highlight" name="code"><;?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>;
</pre>
<p>
<b>Step 6: Run It</b><br />
Start and deploy above web project.</p>
<p> <i>http://localhost:8080/mygradleapp/</i></p>
<p></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://50.87.249.200/~dineshon/wp-content/uploads/2015/01/gradle-spring-output.png"/></div>
<p>
Step 7. Create WAR File<br />
Clean and build a WAR file with the following command :</p>
<p> <i>$ gradle clean build<br />
:clean<br />
:compileJava<br />
:processResources<br />
:classes<br />
:war<br />
:assemble<br />
:compileTestJava UP-TO-DATE<br />
:processTestResources UP-TO-DATE<br />
:testClasses UP-TO-DATE<br />
:test UP-TO-DATE<br />
:check UP-TO-DATE<br />
:build</p>
<p>BUILD SUCCESSFUL</p>
<p>Total time: 4.012 secs</i></p>
<p> The generated WAR file is located at the buildlibs folder.</p>
<p> <b>${Project}buildlibsmygradleapp.war</b></p>
<p> <b>Download Source Code</b></p>
<p> <b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/mygradleapp.zip?attredirects=0&;d=1">mygradleapp.zip</a></b></p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/modelattribute-in-spring-mvc/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/thymeleaf-vs-jsp-spring-mvc-view-layer/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '168'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…