Creating Spring MVC Project in Eclipse Using Maven

This tutorial shows steps for creating Spring MVC Project in Eclipse Using Maven. As you know that Maven and Eclipse are using widely in java programming. The example will show step by step with the illustrated pictures.

Table of contents:
1. Create a Maven Project
2. Maven dependencies
3. Configure Spring MVC with Java Annotation Configuration
4. Configure the Dispatcher Servlet
5. Create the Controller
6. Create the View
7. Deploy Spring MVC Project in Eclipse Using Maven

Other interesting posts you may like

Now, we are ready to follow the step by step for creating Spring MVC Project in Eclipse Using Maven like below:

Create a Maven Project

Create a Maven project on Eclipse by following the below steps:
1. On Eclipse IDE, open the following popup by choosing the path File -> New -> Other
spring-mvc-maven-eclipse-example-new

2. Select workspace

Creating Spring MVC Project in Eclipse Using Maven workspace

3. Select webapp

Creating Spring MVC Project in Eclipse Using Maven webapp

4. Specify Archetype parameters that includes Group Id and Artifact Id

Creating Spring MVC Project in Eclipse Using Maven groupid

GroupId identifies the project uniquely across all projects, so we need to enforce a naming schema. ArtifactId is the name of the jar without version.

Click button Finish, the Maven project is created, in the Eclipse Navigator the project should look like the following:

Creating Spring MVC Project in Eclipse Using Maven project

Maven dependencies
Currently, We have only the skeleton of maven project. We have to add Spring library and other necessary library. The dependencies should be added in the pom.xml like below.

Configure Spring MVC with Java Annotation Configuration
Here, we create WebMvcConfigurerAdapter by using Java Annotation configuration. It replaces for the web.xml file.
You can study deeply about the WebMvcConfigurerAdapter class by refering to the post Spring MVC Annotation Example.

Configure the Dispatcher Servlet
The DispatcherServlet is used to dispatch the requests to the appropriate controller methods. We configure it by extending the AbstractAnnotationConfigDispatcherServletInitializer. This class replaces for the normal spring configuration file.

Create the Controller

Create the View
Finally, we create a form view and another view that displays form data. The form view looks like the following:
employee.jsp

and the another view looks like the following:
success.jsp

Completely Project structure
Almost done, we have the completely project with structure like this image

Creating Spring MVC Project in Eclipse Using Maven completely

Deploy Spring MVC Project in Eclipse Using Maven

We have finished for Creating Spring MVC Project in Eclipse Using Maven. It’s time we package the project into war file by using maven. Right click the project and select Run As -> Maven install. Then we have the spring-mvc-maven. war file in the target directory in the project. The file name is set in the pom.xml file.

We can run spring application by copying the above war file into webapps directory of Tomcat 8 or right click on the project in Eclipse Navigator and select Run As -> Run on Server. Accessing the URL http://localhost:8080/spring-mvc-maven/ and the screen should display such as below

Creating Spring MVC Project in Eclipse Using Maven display

We try to input data on the form like the picture

Creating Spring MVC Project in Eclipse Using Maven input

Then click button Submit, the screen displays our input like below

Creating Spring MVC Project in Eclipse Using Maven input result

That’s all on the tutorial Creating Spring MVC Project in Eclipse Using Maven.

Download complete source code of example, please click link below


SpringMVC-Maven-Eclipse-Example.zip (606 downloads)

5 thoughts on “Creating Spring MVC Project in Eclipse Using Maven”

Leave a Comment

*

Please share it if you found this useful
Hide Buttons