掌握Spring MVC,实战案例教你轻松入门

发布时间:2025-06-08 02:37:48

引言

Spring MVC 是一个风行的 Java Web 开辟框架,它遵守 MVC(Model-View-Controller)计划形式,可能帮助开辟者疾速构建高品质的 Web 利用顺序。本文将经由过程一系列实战案例,带你从零开端,轻松控制 Spring MVC。

一、Spring MVC 简介

Spring MVC 是 Spring 框架的一部分,它供给了丰富的功能跟易于利用的接口,使得 Web 开辟变得愈加简单。Spring MVC 的核心组件包含:

  • DispatcherServlet:前端把持器,担任接收恳求并挪用响应的处理器。
  • HandlerMapping:将恳求映射四处理器上。
  • HandlerAdapter:适配器,将恳求委托给处理器。
  • Controller:处理器,处理恳求并生成呼应。
  • ViewResolver:视图剖析器,将模型数据转达给视图。

二、实战案例:创建一个简单的 Spring MVC 利用

2.1 创建项目

  1. 利用 Maven 创建一个 Web 项目。
  2. pom.xml 文件中增加 Spring MVC 依附。
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.23</version>
    </dependency>
</dependencies>

2.2 设置 DispatcherServlet

  1. web.xml 文件中设置 DispatcherServlet。
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

2.3 创建把持器

  1. 创建一个把持器类,比方 HelloController
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello() {
        return "hello";
    }
}

2.4 创建视图

  1. src/main/webapp/WEB-INF/views 目录下创建一个名为 hello.jsp 的 JSP 页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello, World!</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

2.5 运转项目

  1. 启动 Tomcat 效劳器。
  2. 在浏览器中拜访 http://localhost:8080/hello,应当会看到 “Hello, World!” 的信息。

三、实战案例:利用注解停止恳求映射

3.1 创建把持器

  1. 创建一个把持器类,比方 AnnotationController
@Controller
public class AnnotationController {

    @RequestMapping("/annotation")
    public String sayAnnotation() {
        return "annotation";
    }
}

3.2 创建视图

  1. src/main/webapp/WEB-INF/views 目录下创建一个名为 annotation.jsp 的 JSP 页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Annotation, World!</title>
</head>
<body>
    <h1>Annotation, World!</h1>
</body>
</html>

3.3 运转项目

  1. 在浏览器中拜访 http://localhost:8080/annotation,应当会看到 “Annotation, World!” 的信息。

四、实战案例:利用 Spring MVC 处理表单提交

4.1 创建把持器

  1. 创建一个把持器类,比方 FormController
@Controller
public class FormController {

    @RequestMapping("/form")
    public String showForm() {
        return "form";
    }

    @PostMapping("/submit")
    public String submitForm(@ModelAttribute("user") User user) {
        // 处理表单提交逻辑
        return "success";
    }
}

4.2 创建视图

  1. src/main/webapp/WEB-INF/views 目录下创建一个名为 form.jsp 的 JSP 页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Form Example</title>
</head>
<body>
    <form:form action="submit" modelAttribute="user">
        <div>
            <label for="name">Name:</label>
            <form:input path="name" id="name"/>
        </div>
        <div>
            <label for="email">Email:</label>
            <form:input path="email" id="email"/>
        </div>
        <div>
            <input type="submit" value="Submit"/>
        </div>
    </form:form>
</body>
</html>
  1. 创建一个名为 success.jsp 的 JSP 页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Form Submission Success</title>
</head>
<body>
    <h1>Form submitted successfully!</h1>
</body>
</html>

4.3 运转项目

  1. 在浏览器中拜访 http://localhost:8080/form,填写表单并提交,应当会看到 “Form submitted successfully!” 的信息。

五、总结

经由过程以上实战案例,信赖你曾经对 Spring MVC 有了必定的懂得。在现实开辟中,Spring MVC 可能帮助你更疾速、更高效地构建 Web 利用顺序。祝你进修高兴!