用久了SpringBoot,深受其约定大于配置的便利性 “毒害” 以后,我想回归到SpringMVC时代,看看SpringMVC开发模式中用户是如何参与的。本文就来体验一下SpringMVC时代开发的流程。html
注: 本文首发于 My 公众号 CodeSheep ,可 长按 或 扫描 下面的 当心心 来订阅 ↓ ↓ ↓前端
一个典型的SpringMVC请求流程如图所示,详细分为12个步骤:java
整个过程清晰明了,下面咱们将结合实际实验来理解这整个过程。web
实验环境以下:spring
这里我是用IDEA来搭建的基于Maven的SpringMVC项目,搭建过程再也不赘述,各类点击而且下一步,最终建立好的项目架构以下:编程
使用了SpringMVC,则全部的请求都应该交由SpingMVC来管理,即要将全部符合条件的请求拦截到SpringMVC的专有Servlet上。c#
为此咱们须要在 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来进行处理bash
从上一步的配置能够看到,咱们定义的mvc-dispatcher Servlet依赖于配置文件 mvc-dispatcher.xml
,在本步骤中咱们须要在其中添加三个方面的配置服务器
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
复制代码
SpringMVC的处理器映射器有多种,这里的使用的BeanNameUrlHandlerMapping其映射规则是将bean的name做为url进行处理
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
复制代码
SpringMVC的处理器适配器也有多种,这里的使用的SimpleControllerHandlerAdapter是Controller实现类的适配器类,其本质是执行Controller中的handleRequest方法。
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
复制代码
这里配置了InternalResourceViewResolver视图解析器后,其会根据controller方法执行以后返回的ModelAndView中的视图的具体位置,来加载对应的界面并绑定数据
这里模拟的是一个打印学生名单的Service,咱们编写的控制器须要将查询到的学生名单数据经过ModelAndView渲染到指定的JSP页面中
public class TestController implements Controller {
private StudentService studentService = new StudentService();
@Override
public ModelAndView handleRequest( HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 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;
}
}
复制代码
这里的视图文件是一个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>
复制代码
结合本步骤和上一步骤,视图和控制器都已编写完成,因为咱们以前配置的处理器映射器为:BeanNameUrlHandlerMapping,所以接下来咱们还须要在mvc-dispatcher.xml文件中配置一个可被url映射的controller的bean,供处理器映射器BeanNameUrlHandlerMapping查找:
<bean name="/test.action" class="cn.codesheep.controller.TestController" />
复制代码
启动Tomcat服务器,而后浏览器输入:
http://localhost:8080/test.action
复制代码
数据渲染OK。
备注:固然本文所使用的全是非注解的配置方法,即须要在XML中进行配置而且须要遵循各类实现原则。而更加通用、主流的基于注解的配置方法将在后续文章中详述。
呼,长舒一口气,这么个小Demo用SpringMVC完成的话,各类XML配置了半天,真麻烦啊,算了,仍是回SpringBoot好了!
做者更多的SpringBt实践文章在此:
若是有兴趣,也能够抽点时间看看做者一些关于容器化、微服务化方面的文章: