Spring 5 MVC and Java-Based Configuration


Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. In this post we will build a basic Spring MVC 5 Web Application with Bootstrap. we will see how to configure a Spring MVC application without using a web.xml. We will use Java-based configuration.

Project Structure


Create a Pom.xml for the Required Libraries

We will use a simple Maven web project. Maven will resolve and manage all the dependencies automatically. Below is the pom.xml file for our project. Notice here is the maven-war-plugin declaration. As we will be completely removing web.xml, we will need to configure this plugin in order to avoid maven failure to build war package.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





       Web application development utilities applicable to both Servlet and Portlet Environments
        (depends on spring-core, spring-beans, spring-context)
       Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring           (org.springframework.web.*)





Create a Controller Class

@Controller annotation on class name declares this class as spring bean and The @RequestMapping annotation maps the URL's onto particular classes or methods.In this example, we populate the Model map- which is basically a map for the request attributes with a key called message and a simple string value.

package com.onrugi.controller;

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

public class AppController {

      @RequestMapping(method = RequestMethod.GET)
      public String index(ModelMap model){
           model.addAttribute("message", "Spring MVC Java Configuration Example");
           return "index";


Spring MVC with Java Configuration

@ComponentScan() - Scan starts from base package and registers all controllers, repositories, service, beans, etc.

@EnableWebMvc - Enable Spring MVC-specific annotations like @Controller

@Configuration - Treat as the configuration file for Spring MVC-enabled applications.the @Bean tag to register ViewResolver.

package com.onrugi.configuration;

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.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;


public class AppConfig implements WebMvcConfigurer{

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

     * Configure ResourceHandlers to serve static resources like CSS/ Javascript etc...
     public void addResourceHandlers(ResourceHandlerRegistry registry) {


Configuring the Dispatcher Servlet

DispatcherServlet, which acts as the FrontController of the Spring MVC application.

package com.onrugi.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

        protected Class<?>[] getRootConfigClasses() {
             return null;

        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[] { AppConfig.class };

        protected String[] getServletMappings() {
             return new String[] { "/" };


Create a new folder named views under WEB-INF and add in a Simple JSP page welcome.jsp (WEB-INF/views/index.jsp) to simply access the model value sent from controller.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">

<title>Welcome To sprring MVC</title>

<!-- Custom styles for this template -->
<link href="<c:url value='/assets/css/bootstrap.css' />" rel="stylesheet"></link>
<link href="<c:url value='/assets/css/starter-template.css'/>" rel="stylesheet"></link>



<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="<c:url value='/ ' />">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>

<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="<c:url value='/' />">Home <span class="sr-only">(current)</span></a>

<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>

<main role="main" class="container">

<div class="starter-template">
<h1>Spring MVC 5 + Bootstrap starter template</h1>
<p class="lead">${message}</p>

</main><!-- /.container -->

<script src="<c:url value='/assets/dist/jquery.js' />"> </script>
<script src="<c:url value='/assets/js/bootstrap.js' />"> </script>



URL: http://localhost:8080/Spring-MVC-Java-Configuration/


Download Source Code

The source code is available on my GitHub repository.Download Source Code .


  1. JerderE says:

    Levaquin No Prescription With Free Shipping Store Wisconsin https://buyciallisonline.com/ - buy cialis generic online Achat De Viagra France cialis generic buy Where To Buy Metronidazole

  2. JerderE says:

    Viagra Kaufen Cialis Bestellen https://buycialisuss.com/ - cialis reviews Kamagra Oral Jelly Versand Cialis Amoxicillin Clavu

  3. JerderE says:

    Flex Keflex 250mg https://buycialisuss.com/ - Cialis prices for cialis Buy Cialis Zithromax Online

  4. Agrirllog says:

    Viagra For Man https://bbuycialisss.com/ - Buy Cialis Viagra A Change Ma Vie cialis 5mg best price Was Bedeutet Levitra

  5. generic cialis says:

    Cephalexin Dosage EdupepCeap https://bbuycialisss.com/# - order cialis online Empagmagearl Dutasteride Avidart RonnaRothbut Buy Cialis locadayday Ed Meds Online Canada

Leave your Comment