Thanks to new Maven archetypes for Spring and Maven integration plug-in for Eclipse, creating skeleton stuffs for a Spring MVC project has never been easier. Forget the days we have to manually add JARs, create XML files, find and put appropriate Maven dependencies, etc. This tutorial shows you how quickly to create a basic Spring MVC project and get it up and running, using the Maven archetype called spring-mvc-archetype from group Id co.ntier.

Firstly, make sure you have the Maven Integration for WTP (m2e-wtp) plug-in installed in your Eclipse IDE:

Now let’s go through few steps to create a new Spring MVC project using Eclipse IDE. Depending on your installation, the archetype spring-mvc-archetype is installed or not. If not, we’ll see how to download and install it the following steps.

In Eclipse IDE, click menu File > New > Maven Project (or File > New > Other… > Maven Project). The New Maven Project dialog appears:

New Maven Project - select project name and location

Make sure you don’t check the option Create a simple project (skip archetype selection), and click Next. In the next screen, Select an Archetype, you may see a lot of archetypes in the list, so type spring-mvc into the Filter textfield to filter out the list, as shown below:

New Maven Project - select an Archetype - spring-mvc-archetype

If you don’t see the spring-mvc-archetype, click the Add Archetype… button. In the Add Archetype dialog, type the following information:

Specify Archetype for Spring MVC

    • Archetype Group Id: co.ntier
    • Archetype Artifact Id: spring-mvc-archetype
    • Archetype Version: 1.0.2
    • Repository URL: leave empty


Note that you must leave the Repository URL field empty.

Click OK and wait for a moment while Eclipse is downloading the archetype. Finally you would see the spring-mvc-archetype in the list. Select it and click Next. In the next screen, Specify Archetype parameters, enter the following information (for example):

New Maven Project - specify archetype parameters

    • Group Id: MySpringMvc
    • Artifact Id: MySpringMvc
    • Version: leave this as default, 0.0.1-SNAPSHOT.
    • Package: net.codejava.spring
Click Finish and wait a while when Eclipse is generating the project. And finally we have a project structure looks like below:

Spring MVC project structure

As we can see, all the necessary stuffs of a Spring MVC project are generated automatically. Let’s look at the main things:

    • MvcConfiguration.java: this configuration class uses annotations in replacement of XML since annotations are preferred. It configures a default view resolver and a resource handler. We don’t see Spring application context XML file any more.
    • HomeController.java: this controller is generated to handler the default URL mapping (/). Look at its code, it redirects the user to the “home” view.
    • home.jsp: This JSP page will be displayed when the user access this application using the default URL (/). This is simply a hello world page.
    • web.xml: bootstraps the Spring context loader listener and Spring dispatcher servlet.
    • pom.xml: All dependencies for a Spring MVC application are listed here.
Now, without writing any code so far, we can get this basic Spring MVC application up and running. Drag the project into a server, e.g. Tomcat v7.0, in Servers view, and then start the server. Then open the Internal Web Browser view and type the following URL:

http://localhost:8080/MySpringMvc

Hit Enter, we should see the “Hello World!” page as seen below:

Run Spring MVC application

Important Notes:

The spring-mvc-archetype from co.ntier was created long time ago so it comes with old versions of Java (1.6), Spring framework (3.1.0) with cglib 2.2.2, Servlet 2.4, etc. So it's strongly recommended to update the pom.xml file for newer versions, for example:

<project ...>
	...
	<properties>
		<java.version>1.8</java.version>
		<spring.version>5.2.3.RELEASE</spring.version>
	</properties>

	<dependencies>
		...
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		...
	</dependencies>
	...
</project>
If you use Spring 5, you need to remove the cglib dependency as it is no longer needed, and update the MvcConfiguration class to implement the WebMvcConfigurer interface like this:

...
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class MvcConfiguration implements WebMvcConfigurer {
	...
}
Watch this video to see the steps visually:

So to conclude, using the Maven archetype spring-mvc-archetype definitely saves us a lot of time in configuring the fundamental stuffs, avoiding common mistakes and following best practices. But basically, you still need to understand what the generated stuffs do, e.g. annotation types, bootstrap code in web.xml and dependencies in pom.xml. Although you may take more than one minute to read this till now, following the above steps take just a minute :). Enjoy coding!

 

Related Spring Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#49MATHIVANAN2021-09-01 10:46
K

One of the Nice Article

Good Work Nam Ma Minh

Please Send me the Notifications

Thanks

Thanks
Quote
#48yahia2020-05-31 05:17
error message :
Can't resolve Archetype co.ntier:spring-mvc-archetype:1.0.2
org.eclipse.core.runtime.CoreException: Could not resolve artifact co.ntier:spring-mvc-archetype:pom:1.0.2
Quote
#47vishnu2020-04-27 02:37
Sir,
Can you please tell me how to debug a maven project?
Quote
#46Karim2017-09-14 06:04
Hello all developpers,
after this configuration to add archetype i was blocked with a message box
Unable to create project from archetype
Quote
#45Sonu2017-06-30 14:13
Having problem with spring mcv archetype. The error i get is " Can't resolve Archetype co.ntier:spring-mvc-archetype:1.0.2
org.eclipse.core.runtime.CoreException: Could not resolve artifact co.ntier:spring-mvc-archetype:pom:1.0.2 ". How may i resolve this problem? Thanks. Warm regards.
Quote