Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Setting Up Spring MVC

Setting up Spring MVC involves configuring the DispatcherServlet, creating controller classes, and setting up view templates. This guide covers the essential steps to get a Spring MVC application up and running.

Steps to Set Up Spring MVC

  • Configure the DispatcherServlet in the web.xml file.
  • Create a Spring configuration file.
  • Create controller classes.
  • Set up view templates.

Step 1: Configure the DispatcherServlet

The DispatcherServlet is the front controller in Spring MVC. You need to configure it in the web.xml file:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
         version="3.1">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Step 2: Create a Spring Configuration File

Create a Spring configuration file to define beans and enable component scanning:

dispatcher-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.example.springmvc.controllers" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Step 3: Create Controller Classes

Create controller classes to handle user requests. Annotate these classes with @Controller and map request URLs using @RequestMapping or @GetMapping:

HomeController.java

// HomeController.java
package com.example.springmvc.controllers;

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

@Controller
public class HomeController {
    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("message", "Welcome to Spring MVC!");
        return "home";
    }
}

Step 4: Set Up View Templates

Create JSP files or other view templates to render the response. Place these files in the configured directory (e.g., /WEB-INF/views/):

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>${message}</h1>
</body>
</html>

Running the Application

Deploy your application to a servlet container such as Apache Tomcat. Access the application in a web browser using the appropriate URL (e.g., http://localhost:8080/). You should see the welcome message rendered by the home.jsp view.

Key Points

  • Configure the DispatcherServlet in the web.xml file.
  • Create a Spring configuration file to define beans and enable component scanning.
  • Create controller classes annotated with @Controller.
  • Set up view templates (e.g., JSP files) to render the response.
  • Deploy the application to a servlet container and access it via a web browser.

Conclusion

Setting up Spring MVC involves configuring the DispatcherServlet, creating controller classes, and setting up view templates. By following these steps, you can quickly get a Spring MVC application up and running. Understanding these basic steps lays the foundation for building more complex applications. Happy coding!