Introduction to Spring MVC
Spring MVC (Model-View-Controller) is a framework within the Spring Framework that facilitates building web applications. It follows the MVC design pattern, which separates the application into three interconnected components: the model, the view, and the controller. This overview covers the key concepts and basic setup of Spring MVC.
Key Concepts of Spring MVC
- Model: Represents the application data and business logic.
- View: Represents the presentation layer, typically rendered as HTML.
- Controller: Handles user requests and maps them to appropriate service methods.
- DispatcherServlet: The central servlet that dispatches requests to controllers.
Setting Up Spring MVC
To set up a Spring MVC project, you need to configure the DispatcherServlet
and create controller classes. Here is an example of the basic setup:
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>
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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<!-- Scan for controller classes -->
<context:component-scan base-package="com.example.springmvc.controllers" />
<!-- View resolver for JSP pages -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
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";
}
}
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
- Model: Represents application data and business logic.
- View: Represents the presentation layer, typically rendered as HTML.
- Controller: Handles user requests and maps them to appropriate service methods.
- DispatcherServlet: The central servlet that dispatches requests to controllers.
- Set up the
DispatcherServlet
inweb.xml
. - Configure Spring MVC using a configuration file (e.g.,
dispatcher-config.xml
). - Create controller classes annotated with
@Controller
. - Create view templates (e.g., JSP pages) to render the response.
Conclusion
Spring MVC is a powerful framework for building web applications. By following the MVC design pattern, it promotes separation of concerns and provides a structured way to develop web applications. Understanding the key concepts and setting up a basic Spring MVC project lays the foundation for building more complex applications. Happy coding!