Spring MVC: Creating a Project from Scratch using Maven

If you don’t want to use Spring STS to create your Spring MVC project, you can, of course, create it from the scratch. And here is what we will need to do:

  • Create a java web project using maven
  • Test it using Jetty
  • Transform it to a Spring MVC project

For the source code, you can download it here: https://github.com/nxhoaf/spring-mvc-from-scratch. And now, let’s start:

1. Create a java j2ee project using maven:

mvn archetype:generate

When asked for the version, just enter to choose the default one (1.0-SNAPSHOT).  After that, we should have a project structure like below:

    ├── pom.xml
    └── src
        └── main
            ├── resources
            └── webapp
                ├── index.jsp
                └── WEB-INF
                    └── web.xml

Next, we will convert it to eclipse project and import it to your eclipse ide. This step is optional, if you don’t want to use eclipse, feel free to go ahead.

mvn eclipse:eclipse

2. Test it using jetty server

– Add some configurations:

            <!-- To enable jetty -->

        <!-- Servlet and JSP -->
            <!-- <scope>provided</scope> -->
            <!-- <scope>provided</scope> -->

run it:

mvn clean install
mvn jetty:run

Then, go to http://localhost:8080/spring-mvc-from-scratch/, you should she the hello world message.

3. Transform the project to a Spring MVC project

– Add the FrontController and the listener in the xml file:


    <!-- Define Front Controller for Spring -->


– Then, create an applicationContext.xml in WEB-INF with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <!-- Handle annotation driven request mapping -->
    <mvc:annotation-driven />

    <context:component-scan base-package="com.gmail.nxhoaf" />
       <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />


– Next, import the following Spring MVC dependencies to your pom.xml

    <!-- Define some properties -->

        <!-- Spring -->

– Then, we create the first Spring Hello World Controller:

package com.gmail.nxhoaf;

import java.util.Date;
import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class HelloWorldController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        Date date = new Date();
        model.addAttribute("serverTime", date );
        return "helloWorld";

And finally, create the helloWorld.jsp in WEB-INF/views

<%@ page isELIgnored="false" %>

<h2>Hello World at: ${serverTime}</h2>

Now, you can test your work at: http://localhost:8080/spring-mvc-from-scratch/


This entry was posted in Java. Bookmark the permalink.

1 Response to Spring MVC: Creating a Project from Scratch using Maven

  1. Pingback: Spring MVC: Return a JSON object | nxhoaf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s