

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

一个典型的 Spring MVC 请求流程如图所示,详细分为 12 个步骤:
整个过程清晰明了,下面我们将结合实际实验来理解这整个过程。
实验环境如下:
这里我是用 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 来进行处理
从上一步的配置可以看到,我们定义的 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=" 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。
由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!