Gradle Spring MVC Web Project Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">In this tutorial we explain the spring mvc project and it is managed by Gradle&period; For this Gradle Spring MVC Web Project Example required following technologies&period;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><i><b>gradle-2&period;2&period;1-all&period;zip<&sol;b><&sol;i><&sol;li>&NewLine;<li><i><b>Spring 4&period;0&period;6&period;RELEASE<&sol;b><&sol;i><&sol;li>&NewLine;<li><i><b>STS<&sol;b><&sol;i><&sol;li>&NewLine;<&sol;ul>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"http&colon;&sol;&sol;50&period;87&period;249&period;200&sol;~dineshon&sol;wp-content&sol;uploads&sol;2015&sol;01&sol;gradle&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;<b>Step 1&period;<&sol;b> Add Gradle plugin to STS IDE<&sol;p>&NewLine;<p> <b>Step 2&period;<&sol;b> Create Project Structure for Dynamic Web Project As following&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"http&colon;&sol;&sol;50&period;87&period;249&period;200&sol;~dineshon&sol;wp-content&sol;uploads&sol;2015&sol;01&sol;gradle-spring&period;png" &sol;><&sol;div>&NewLine;<p>&NewLine;<b>Step 3&period;<&sol;b> Gradle Build File<br &sol;>&NewLine;<b>Build&period;gradle<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">apply plugin&colon; 'java'&NewLine;apply plugin&colon; 'war'&NewLine;apply plugin&colon; 'eclipse'&NewLine;&NewLine;repositories &lbrace;&NewLine; mavenCentral&lpar;&rpar;&NewLine;&rcub;&NewLine; &NewLine;dependencies &lbrace;&NewLine; providedCompile 'javax&period;servlet&colon;servlet-api&colon;2&period;5'&NewLine; compile 'org&period;springframework&colon;spring-webmvc&colon;4&period;0&period;0&period;RELEASE'&NewLine; compile 'org&period;thymeleaf&colon;thymeleaf-spring4&colon;2&period;1&period;4&period;RELEASE'&NewLine; compile 'org&period;thymeleaf&colon;thymeleaf&colon;2&period;1&period;4&period;RELEASE'&NewLine; runtime 'javax&period;servlet&colon;jstl&colon;1&period;1&period;2'&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b>Step 4<&sol;b>&period; Go to the project folder&comma; same level with build&period;gradle&comma; issue the following command &colon;<&sol;p>&NewLine;<p><i>&dollar; gradle cleanEclipse eclipse<&sol;i><&sol;p>&NewLine;<p>Now&comma; you can import the project into STS IDE&period;<&sol;p>&NewLine;<p><b>or<&sol;b><&sol;p>&NewLine;<p><b>Step 4&period;<&sol;b> Right click on the project and goto the configure and click convert gradle project&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"http&colon;&sol;&sol;50&period;87&period;249&period;200&sol;~dineshon&sol;wp-content&sol;uploads&sol;2015&sol;01&sol;convert-gradle-project&period;png" &sol;><&sol;div>&NewLine;<p>&NewLine;<b>Step 5<&sol;b>&period; Spring MVC Files Code <&sol;p>&NewLine;<p><b>HomeController&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;doj&period;controller&semi;&NewLine;&NewLine;import org&period;springframework&period;stereotype&period;Controller&semi;&NewLine;import org&period;springframework&period;ui&period;ModelMap&semi;&NewLine;import org&period;springframework&period;web&period;bind&period;annotation&period;RequestMapping&semi;&NewLine;import org&period;springframework&period;web&period;bind&period;annotation&period;RequestMethod&semi;&NewLine;&NewLine;&commat;Controller&NewLine;public class HomeController &lbrace;&NewLine; &NewLine; &commat;RequestMapping&lpar;value&equals;"&sol;"&comma; method &equals; RequestMethod&period;GET&rpar;&NewLine; public String printWelcome&lpar;ModelMap model&rpar; &lbrace;&NewLine; model&period;addAttribute&lpar;"message"&comma; "Spring 4 MVC Hello Gradle World&excl;&excl;&excl; This is a Thymeleaf template "&rpar;&semi;&NewLine; return "home"&semi;&NewLine; &NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>home&period;jsp<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" type&equals;"code">&lt&semi;html&gt&semi;&NewLine;&lt&semi;body&gt&semi;&NewLine; &lt&semi;h1&gt&semi;Message &colon; &dollar;&lbrace;message&rcub;&lt&semi;&sol;h1&gt&semi; &NewLine;&lt&semi;&sol;body&gt&semi;&NewLine;&lt&semi;&sol;html&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>mygradleapp-servlet&period;xml<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;beans xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" &NewLine; xmlns&colon;context&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context" &NewLine; xmlns&colon;p&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;p" &NewLine; xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" &NewLine; xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&sol;spring-beans-4&period;0&period;xsd &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context&sol;spring-context-4&period;0&period;xsd"&gt&semi; &NewLine; &NewLine; &lt&semi;context&colon;component-scan base-package&equals;"com&period;doj&period;controller" &sol;&gt&semi; &NewLine; &NewLine; &lt&semi;bean id&equals;"viewResolver" class&equals;"org&period;springframework&period;web&period;servlet&period;view&period;InternalResourceViewResolver"&gt&semi; &NewLine; &lt&semi;property name&equals;"prefix"&gt&semi; &NewLine; &lt&semi;value&gt&semi;&sol;WEB-INF&sol;view&sol;&lt&semi;&sol;value&gt&semi; &NewLine; &lt&semi;&sol;property&gt&semi; &NewLine; &lt&semi;property name&equals;"suffix"&gt&semi; &NewLine; &lt&semi;value&gt&semi;&period;jsp&lt&semi;&sol;value&gt&semi; &NewLine; &lt&semi;&sol;property&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine;&lt&semi;&sol;beans&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>web&period;xml<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi;&NewLine;&lt&semi;web-app xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" xmlns&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee" xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;3&lowbar;0&period;xsd" id&equals;"WebApp&lowbar;ID" version&equals;"3&period;0"&gt&semi;&NewLine; &lt&semi;display-name&gt&semi;mygradleapp&lt&semi;&sol;display-name&gt&semi;&NewLine; &lt&semi;servlet&gt&semi;&NewLine; &lt&semi;servlet-name&gt&semi;mygradleapp&lt&semi;&sol;servlet-name&gt&semi;&NewLine; &lt&semi;servlet-class&gt&semi;&NewLine; org&period;springframework&period;web&period;servlet&period;DispatcherServlet&NewLine; &lt&semi;&sol;servlet-class&gt&semi;&NewLine; &lt&semi;load-on-startup&gt&semi;1&lt&semi;&sol;load-on-startup&gt&semi;&NewLine; &lt&semi;&sol;servlet&gt&semi;&NewLine; &lt&semi;servlet-mapping&gt&semi;&NewLine; &lt&semi;servlet-name&gt&semi;mygradleapp&lt&semi;&sol;servlet-name&gt&semi;&NewLine; &lt&semi;url-pattern&gt&semi;&sol;&lt&semi;&sol;url-pattern&gt&semi;&NewLine; &lt&semi;&sol;servlet-mapping&gt&semi;&NewLine; &NewLine;&lt&semi;&sol;web-app&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Step 6&colon; Run It<&sol;b><br &sol;>&NewLine;Start and deploy above web project&period;<&sol;p>&NewLine;<p> <i>http&colon;&sol;&sol;localhost&colon;8080&sol;mygradleapp&sol;<&sol;i><&sol;p>&NewLine;<p><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"http&colon;&sol;&sol;50&period;87&period;249&period;200&sol;~dineshon&sol;wp-content&sol;uploads&sol;2015&sol;01&sol;gradle-spring-output&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;Step 7&period; Create WAR File<br &sol;>&NewLine;Clean and build a WAR file with the following command &colon;<&sol;p>&NewLine;<p> <i>&dollar; gradle clean build<br &sol;>&NewLine;&colon;clean<br &sol;>&NewLine;&colon;compileJava<br &sol;>&NewLine;&colon;processResources<br &sol;>&NewLine;&colon;classes<br &sol;>&NewLine;&colon;war<br &sol;>&NewLine;&colon;assemble<br &sol;>&NewLine;&colon;compileTestJava UP-TO-DATE<br &sol;>&NewLine;&colon;processTestResources UP-TO-DATE<br &sol;>&NewLine;&colon;testClasses UP-TO-DATE<br &sol;>&NewLine;&colon;test UP-TO-DATE<br &sol;>&NewLine;&colon;check UP-TO-DATE<br &sol;>&NewLine;&colon;build<&sol;p>&NewLine;<p>BUILD SUCCESSFUL<&sol;p>&NewLine;<p>Total time&colon; 4&period;012 secs<&sol;i><&sol;p>&NewLine;<p> The generated WAR file is located at the buildlibs folder&period;<&sol;p>&NewLine;<p> <b>&dollar;&lbrace;Project&rcub;buildlibsmygradleapp&period;war<&sol;b><&sol;p>&NewLine;<p> <b>Download Source Code<&sol;b><&sol;p>&NewLine;<p> <b><a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;mygradleapp&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1">mygradleapp&period;zip<&sol;a><&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;modelattribute-in-spring-mvc&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;thymeleaf-vs-jsp-spring-mvc-view-layer&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '168'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago