Spring MVC
Spring MVC (Model-View-Controller) is a comprehensive framework for building web applications and RESTful web services in Java. It provides a flexible and powerful way to handle web requests, manage application logic, and generate dynamic views.
Key Components of Spring MVC
- DispatcherServlet: The central component that dispatches requests to the appropriate handlers.
- Controllers: Handle web requests and return model data and views.
- Models: Hold the data that will be displayed in the view.
- Views: Render the model data as a web page or other format.
- ViewResolvers: Resolve the logical view names returned by controllers to actual view implementations.
- HandlerMappings: Map web requests to handler methods.
Setting Up Spring MVC
To set up a Spring MVC application, you need to configure the DispatcherServlet, define controllers, and set up views. Here is a basic setup:
web.xml Configuration
<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/dispatcher-servlet.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-servlet.xml Configuration
<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">
<context:component-scan base-package="com.example"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Creating a Controller
Controllers handle web requests and return model data and views. Here is an example of a simple controller:
// HomeController.java
package com.example;
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";
}
}
Creating a View
Views render the model data as web pages or other formats. Here is an example of a simple JSP view:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
Handling Form Data
Spring MVC makes it easy to handle form data. Here is an example of a form and a controller that handles the form submission:
Form View (form.jsp)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="submitForm" method="post">
Name: <input type="text" name="name"/> <br/>
Age: <input type="text" name="age"/> <br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
Form Controller
// FormController.java
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;
@Controller
public class FormController {
@PostMapping("/submitForm")
public String submitForm(@RequestParam("name") String name, @RequestParam("age") int age, Model model) {
model.addAttribute("name", name);
model.addAttribute("age", age);
return "result";
}
}
Result View (result.jsp)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Result</title>
</head>
<body>
<h1>Form Submission Result</h1>
Name: ${name} <br/>
Age: ${age} <br/>
</body>
</html>
Key Points
- Spring MVC is a comprehensive framework for building web applications and RESTful web services.
- Key components include DispatcherServlet, controllers, models, views, ViewResolvers, and HandlerMappings.
- Spring MVC applications are configured using DispatcherServlet, controller classes, and view resolvers.
- Controllers handle web requests, manage application logic, and return views.
- Views render the model data as web pages or other formats.
- Spring MVC supports handling form data easily with @RequestParam and model attributes.
Conclusion
Spring MVC provides a powerful and flexible way to build web applications and RESTful web services. By leveraging its components and features, developers can create robust and maintainable web applications with ease. Happy coding!