Getting Started with Maven Based Spring MVC 5 Web Application

  • The Spring Framework is an Open Source application framework and inversion of control container for the Java platform.
  • Spring Framework allows allows Java developers to build simple, reliable, and scalable enterprise applications.
  • Latest version of Spring Framework is 5.2.0.RELEASE relased on September 30, 2019
  • Official website is

Step 1: Download Spring Tools 4 for Eclipse from Spring Tools 4 is the next generation of Spring tooling for your favorite coding environment. Largely rebuilt from scratch, it provides world-class support for developing Spring-based enterprise applications

Step 2: Create Maven Project from STS File > New > Maven Project as show below

Select maven-archetype-webapp Artifact Id

Now fill the below details

  • Group id : It uniquely identifies your project, like com.companyname
  • Artifact id : It is name of jar or war without version, like project name
  • Version : Version is used for version control for artifact id.

Once the project is created, the project structure looks like as shown below.

You may get an error showing “javax.servlet.http.HttpServlet” was found in java build path” in index.jsp. To solve this issue you need to set target run time as Apache Tomcat , right click on project -> properties -> target runtimes

Download Tomcat Server, unzip and keep in one folder

Now add Spring Dependency in pom.xml file

<project xmlns="" xmlns:xsi=""
  <name>springmvc5demo Maven Webapp</name>

        <!-- JSTL Dependency -->

        <!-- Servlet Dependency -->

        <!-- JSP Dependency -->

Now create packages under src/main/java as show in the below screenshot

Now Create, under springmvc5demo.config package.

Create under springmvc5demo.model

Create under springmvc5demo.controller

package springmvc5demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@ComponentScan(basePackages = {
    "springmvc5demo.config","springmvc5demo.controller", "springmvc5demo.model"
public class AppConfig {

    public InternalResourceViewResolver resolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        return resolver;

package springmvc5demo.config;


public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    protected Class <?> [] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;

    protected Class <?> [] getServletConfigClasses() {
        return new Class[] {

    protected String[] getServletMappings() {
        return new String[] {

package springmvc5demo.model;
public class Course {
	private String courseName;
	private String courseInstructor;
	public String getCourseName() {
		return courseName;
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	public String getCourseInstructor() {
		return courseInstructor;
	public void setCourseInstructor(String courseInstructor) {
		this.courseInstructor = courseInstructor;

package springmvc5demo.controller;

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

import springmvc5demo.model.Course;

public class CourseController {

    public String handler(Model model) {
        Course cou = new Course();
        cou.setCourseName("Spring Framework");
        model.addAttribute("hello", cou);
        return "hello";
  • First Request goes to dispatcherServlet and it redirects to controller class.
  • Here @Controller depicts that this is our controller class.
  • @RequestMapping is used to map incoming http request to handler method
  • handler() method of will handle GET request from dispatcher.

Create an hello.jsp file under src/main/webapp/WEB-INF/views folder and write the below code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="" prefix="c"%>
<!DOCTYPE html>
<head><%@ page isELIgnored="false" %>
<meta charset="ISO-8859-1">
<title>Spring 5 MVC - Hello World Example</title>

In this article we used Java-based configuration by configuring DispatcherServlet and spring beans configuration.

Tools and Technologies used in this example

  • Spring MVC – 5.1.0 RELEASE
  • JDK – 13
  • Maven – 3.6.1
  • Apache Tomcat – 9.0
  • IDE – STS 4
  • JSTL – 1.2.1



Learn more about Spring Framework in the upcoming blog articles.

Happy Learning!