Spring MVC Configuration Fundamentals

Project Setup and Dependencies

Configure Maven dependencies for Spring MVC web applications:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>spring-web-app</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>

  <properties>
    <spring.version>5.3.18</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
      </plugin>
    </plugins>
  </build>
</project>

Web Deployment Descriptor

Configure the web.xml with dispatcher servlet and encoding filter:

<web-app>
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>
      org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>mvcDispatcher</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/app-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvcDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Spring MVC Configuration

Create application context configuration:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="...>
       
  <context:component-scan base-package="com.example.controllers"/>
  
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
  
  <mvc:annotation-driven/>
</beans>

Controller Implementation

Create request handler with route mapping:

package com.example.controllers;

@Controller
public class MainController {
  
  @RequestMapping(path = "/welcome", method = RequestMethod.GET)
  public String showWelcomePage() {
    return "welcome-view";
  }
}

View Components

Home page with navigation:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<body>
  <h2>Spring MVC Demo</h2>
  <a href="welcome">Go to welcome page</a>
</body>
</html>

Destination view page:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head><title>Welcome Page</title></head>
<body>Operation Successful</body>
</html>

Tags: Spring MVC DispatcherServlet XML Configuration JSP Java Web

Posted on Wed, 27 May 2026 19:10:51 +0000 by ankit17_ag