Spring MVC – Build Project In Java With Gradle From Scratch (No XML)

Hi folks, after a long time I revisited Spring MVC because of some project requirement and in the next moment, I was dealing with all sorts of configurations. If you have worked on Spring MVC before (not SpringBoot), you will know what am I referring to. All sorts of XML files (web.xml and spring-context etc).

And to be clear I hate all that clutter. So, instead of dealing with XMLs, I did it in Java. The package manager I used was Gradle (pretty clear from the heading too (:p)). Okay, so the scope of this article is just to build a project from scratch using Spring MVC dependencies. So do not expect anything more 😀

Install Gradle

This is the first step of the requirement. If you are a Java developer then chances are you already have it in your machine. But if you are new then don’t worry just fire below commands in order and you will be good to go.

There are different ways to install gradle. But the developer way to install any required piece of tool or software is by using the Software Development Kit Manager. I mostly prefer Homebrew and Sdkman for managing the development kit.

If you are working on Mac then it is fairly easy to install. And if you are on windows then you will have to do a little bit more. I will leave the installation to you. You are smart enough to choose the package manager for yourself.

Setting Up Folder Structure

I can very easily do it using some IDE such as Intellij but then you would miss the crux of it all. So, I want to do everything manually and take you through each and every step. Starting from creating the folder structure for your application.

So, to create the folder structure first create a new folder that will be the container for your application (same as project name in your IDE).

After pasting the code in your build.gradle, click on the Import Changes project showed in the bottom right.

All the required spring dependencies will be downloaded and added to the classpath by gradle. In the background, Intellij runs the same command i.e. gradle clean build.

Configure Spring MVC

This used to be the hard part for me. Creating all the XML files and copy-pasting the boilerplate tags at the start and then bootstrapping everything else to make it work.

But after Spring MVC 3, you no longer have to use XML to make it work. You can configure your entire application from the code itself. The biggest advantage – No need to copy-paste or maintain the nasty XML files. Don’t get me wrong, I love XML but not for configuring my application.

Okay so let’s start…

Packages and Classes

There is a total of three configuration classes that are required to bootstrap the entire application.

This class will be used to bootstrap the entire application. You will provide the base packages of your application to scan at the start. You will also tell the Spring MVC to Enable MVC support for the project by importing Spring MVC Configuration from WebMvcConfigurationSupport.

Here, you have noticed that I have created a Bean of InternalResourceViewResolver. It is a way to tell Spring how to get the Servlet View based on the name. In this project, I’m using JSP as application view so I’ve mentioned the same.

Now, let’s build it using gradle by firing below command from the root of the project.

gradle clean build

You will find a war file with the same name that you mentioned in the build.gradle file.

ls build/libs/
helloworld-0.1.0.war

Deploy In Tomcat Server

I hope you are using Intellij because then you can simply follow along the lines.

So to deploy our app in the tomcat server.

Click on Run > Run…

Run Application

Click Edit Configurations…

Edit Configurations

Select Local Tomcat Server

Local Tomcat Server

Do the following:

Provide a name: SpringMvcBoilerplate

Select Application Server: Tomcat 9.0.22

Configure URL: http://localhost:9090/

Tomcat Server Configuration

Click on the Fix button in the bottom right

Fix Button

Select helloworld-0.1.0.war (exploded)

Fix Configurations

Set the Application Context Path

Click Apply

Click Run

Application Configuration

The tomcat server will start and deploy the application to it.

Tomcat Logs

Your browser will automatically open you should see Hello World!

I hope everything worked for you as it worked for me. In case you encountered any error related to the server, code etc… then don’t spend too much time troubleshooting. Simply shoot a comment below and I will help you asap.

I presume if you encounter any error then it might be because of the Tomcat server and deployment. It tends to get tricky sometimes.

Conclusion

So far you have created a Java project, added Spring MVC dependencies and configured entire application without creating an XML file.

I hope you liked the article and if YES then do share it with other budding developers in your circle.

Article By: Varun Shrivastava

Varun Shrivastava is an innovative full stack web developer. He is a passionate blogger and loves to write about philosophy, programming, tech and relationships. He works as a Software Developer. This is his den, you can get in touch with him here anytime you want.

Website Search

Free Ebook Download ($7.06 Value)

Get your free guide on How To Backup your Wordpress site for Free. It's free twice :). Grab it now!

Varun Shrivastava is an innovative full stack web developer. He is a passionate blogger and loves to write about philosophy, programming, tech and relationships. He works as a Software Developer. This is his den, you can get in touch with him here anytime you want.

Subscribe to Our Newsletters

Get your free guide on How To Backup your Wordpress site for Free. It's free twice :). Grab it now!