Java-Spring Web Application – Maven

Here we learn to create a simple web application in Java and Spring using Maven build and dependency management tool. There are several articles written with various alternatives. Java-Spring Web Application – Maven, is the simplest article and it helps in creating the simplest web application.

It just demonstrates integrating Spring MVC using maven. It also demonstrates the capabilities of maven, how we can get rid of the traditional approach of including each jar in the class path by manually searching and downloading it.

First for the impatient programmers:

Complete the following prerequisites and then download the zipped archive, and run the command

to get set go.

Maven will generate a .war (web archive) which you can deploy on your favorite web container. if you deploy this on tomcat, you can access the application by typing in the URL http://localhost:8080/SimpleProject/helloWorld/

Prerequisites for Java-Spring Web Application using Maven

Following is a list of prerequisites to start this project.

1) Java must be set up in the development system.
Read more about installing java on Windows.

2) Maven must be set up in the development system.
Read more about installing maven on Windows.

Detailed Explanation

Use the below command to prepare a project skeleton for a java web project/

Note: To know more about maven archetypes, please visit
Maven Website

You can run the above command after navigating to a directory where you want to create the project.

In the above case a project having name SimpleProject is created in the directory C:\Users\dprasad\ws.
You should see the below directory structure once the command is successfully completed.

|–SimpleProject
|—-pom.xml
|——src
|——–main
|———-resources
|————webapp
|————–index.jsp
|—————-WEB-INF
|——————web.xml

Instructing Maven to use specific versions of Spring and Java Development Kit:

Edit the project’s pom file and add the below code:

maven will use the above properties while building the project.

Maven provides a compile plugin to compile the projects.
Read more about Maven Compiler plugin.

This can be done by editing the pom file and adding the below plugin information anywhere inside the build tag.

As we are building a spring application, it is necessary to inform maven to use spring.
In maven this can be done by adding those jar information as dependencies.

Edit the project’s pom file to add the below code in the dependencies tag:

Now the coding part

Create a directory named java inside the src/main directory to place your java classes.

Create a directory structure inside the java directory to resemble your package structure. In my case the SimpleController.java class is inside a package named in.techieme.springmaven.web

In my case the directory structure at this point is:

|–SimpleSpringMavenProject
|—-pom.xml
|——src
|——–main
|———-java
|————in
|————–techieme
|—————-springmaven
|——————web
|——————–SimpleController.java
|————resources
|————–webapp
|—————-index.jsp
|——————WEB-INF
|———————-web.xml

Now add the below code to the SimpleController.java

We have coded our controller class which will receive the requests from the web. But we still need to inform our web container, i.e. web.xml to send our requests to the SimpleController.

Edit the web.xml file and add the following code in it.

As web applications have their web container which is named as web.xml similarly we have spring container.
The name of the spring container is upon the choice of the user. In our case we put the name as spring-servlet.xml. Just make sure that the name of the file and the name in the entry

in the web.xml must match.

Create a spring configuration file xml (spring-servlet.xml) file and place it in the WEB-INF directory of the project.
Edit the spring-servlet.xml and add the following code:

Stay connected and stay Subscribed