从 Spring Boot 到 Spring MVC(注解方式)

2018-12-26 23:15:32 +08:00
 hansonwang99


概述

在前文《从 SpringBoot 到 SpringMVC (非注解方式)》之中,我们远离了 Spring Boot 的开箱即用与自动配置的便利性后,回归到了淳朴的 Spring MVC 开发时代,但是以非注解的方式来给出的,而本文则以注解方式再度讲述一遍。


Spring MVC 架构模式

一个典型的 Spring MVC 请求流程如图所示,详细分为 12 个步骤:

整个过程清晰明了,下面我们将结合实际实验来理解这整个过程。


Spring MVC 项目搭建

实验环境如下:

这里我是用 IDEA 来搭建的基于 Maven 的 SpringMVC 项目,搭建过程不再赘述,各种点击并且下一步,最终创建好的项目架构如下:


添加前端控制器配置

使用了 SpringMVC,则所有的请求都应该交由 SpingMVC 来管理,即要将所有符合条件的请求拦截到 SpringMVC 的专有 Servlet 上。

为此我们需要在 web.xml 中添加 SpringMVC 的前端控制器 DispatcherServlet:

    <!--springmvc 前端控制器-->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc-dispatcher.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

该配置说明所有符合.action 的 url,都交由 mvc-dispatcher 这个 Servlet 来进行处理


编写 Spring MVC 核心 XML 配置文件

从上一步的配置可以看到,我们定义的 mvc-dispatcher Servlet 依赖于配置文件 mvc-dispatcher.xml,在本步骤中我们需要在其中添加如下的配置

方式一:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

方式二:

<mvc:annotation-driven></mvc:annotation-driven>

编写控制器

由于使用了注解的处理器映射器和处理器适配器,所以不需要在 XML 中配置任何信息,也不需要实现任何接口,只需要添加相应注解即可。

@Controller
public class TestController {

    private StudentService studentService = new StudentService();

    @RequestMapping("/queryStudentsList")
    public ModelAndView handleRequest( ) throws Exception {
        List<Student> studentList = studentService.queryStudents();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("studentList",studentList);
        modelAndView.setViewName("/WEB-INF/views/studentList.jsp");
        return modelAndView;
    }
}

class StudentService {
    public List<Student> queryStudents() {
        List<Student> studentList = new ArrayList<Student>();

        Student hansonwang = new Student();
        hansonwang.setName("hansonwang99");
        hansonwang.setID("123456");

        Student codesheep = new Student();
        codesheep.setName("codesheep");
        codesheep.setID("654321");

        studentList.add(hansonwang);
        studentList.add(codesheep);

        return studentList;
    }
}

为了让注解的处理器映射器和处理器适配器找到注解的 Controllor,有两种配置方式:

方式一:在 xml 中声明 Controllor 对应的 bean

<bean class="cn.codesheep.controller.TestController" />

方式二:使用扫描配置,对某一个包下的所有类进行扫描,找出所有使用 @Controllor 注解的 Handler 控制器类

<context:component-scan base-package="cn.codesheep.controller"></context:component-scan>

编写视图文件

这里的视图文件是一个 jsp 文件,路径为:/WEB-INF/views/studentList.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
    <title>学生名单</title>
</head>
<body>
    <h3>学生列表</h3>
    <table width="300px;" border=1>
        <tr>
            <td>姓名</td>
            <td>学号</td>
        </tr>
        <c:forEach items="${studentList}" var="student" >
            <tr>
                <td>${student.name}</td>
                <td>${student.ID}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

实验测试

启动 Tomcat 服务器,然后浏览器输入:

http://localhost:8080/queryStudentsList.action

数据渲染 OK。


后 记

由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!



1278 次点击
所在节点    Blogger
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/521342

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX