思维导图让 Spring MVC 不再难懂
spring mvc简介与运行原理
Spring的模型-视图-控制器(MVC)框架是围绕一个DispatcherServlet来设计的,这个Servlet会把请求分发给各个处理器,并支持可配置的处理器映射、视图渲染、本地化、时区与主题渲染等,甚至还能支持文件上传。

- (1) Http请求:客户端请求提交到DispatcherServlet。 
- (2) 寻找处理器:由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller。 
- (3) 调用处理器:DispatcherServlet将请求提交到Controller。 
- (4)(5)调用业务处理和返回结果:Controller调用业务逻辑处理后,返回ModelAndView。 
- (6)(7)处理视图映射并返回模型: DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图。 
- (8) Http响应:视图负责将结果显示到客户端。 
主要注解

ContextLoaderListener
在讲ContextLoaderListener之前,首先来了解一下web.xml的作用。
- 一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的。web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。当你的web工程没用到这些时,你可以不用web.xml文件来配置你的Application。 
- 当要启动某个web项目时,服务器软件或容器如(tomcat)会第一步加载项目中的web.xml文件,通过其中的各种配置来启动项目,只有其中配置的各项均无误时,项目才能正确启动。web.xml有多项标签,在其加载的过程中顺序依次为:context-param >> listener >> fileter >> servlet。(同类多个节点以出现顺序依次加载) 
  
而spring mvc启动过程大致分为两个过程:
- ContextLoaderListener初始化,实例化IoC容器,并将此容器实例注册到ServletContext中。 
- DispatcherServlet初始化。 
  
其中ContextLoaderListener监听器它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。
- ContextLoaderListener在web.xml中的配置
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ServletContextListener 接口有两个方法:contextInitialized,contextDestroyed
DispatcherServlet
Spring MVC框架,与其他很多web的MVC框架一样:请求驱动;所有设计都围绕着一个中央Servlet来展开,它负责把所有请求分发到控制器;同时提供其他web应用开发所需要的功能。不过Spring的中央处理器,DispatcherServlet,能做的比这更多。
下图展示了Spring Web MVC的DispatcherServlet处理请求的工作流。熟悉设计模式的朋友会发现,DispatcherServlet应用的其实就是一个“前端控制器”的设计模式(其他很多优秀的web框架也都使用了这个设计模式)。
- 流程图

- 在web.xml中的配置
<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:config/spring/springmvc.xml</param-value>
  </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
其中
- load-on-startup:表示启动容器时初始化该Servlet; 
- url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。 
在Spring MVC中,每个 DispatcherServlet 都持有一个自己的上下文对象 WebApplicationContext,它又继承了根(root)WebApplicationContext 对象中已经定义的所有 bean。这些继承的bean可以在具体的Servlet实例中被重载,在每个 Servlet 实例中你也可以定义其 scope 下的新 bean。
WebApplicationContext 继承自 ApplicationContext,它提供了一些web应用经常需要用到的特性。它与普通的 ApplicationContext 不同的地方在于,它支持主题的解析,并且知道它关联到的是哪个servlet(它持有一个该ServletContext的引用)

DispatcherServlet继承结构
spring mvc同时提供了很多特殊的注解,用于处理请求和渲染视图等。DispatcherServlet初始化的过程中会默认使用这些特殊bean进行配置。如果你想指定使用哪个特定的bean,你可以在web应用上下文WebApplicationContext中简单地配置它们。

其中,常用的ViewResolver的配置。以jsp作为视图为例
配置上传文件限制MultipartResolver
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="maxUploadSize">
	<value>5242880</value>
  </property>
  <property name="defaultEncoding" value="utf-8"></property>
</bean>
applicationContext.xml中的标签

文件上传
前面说到DispatcherServlet中有个特殊的Bean叫MultipartResolver,可用于限制文件的上传大小等。当解析器MultipartResolver完成处理时,请求便会像其他请求一样被正常流程处理。
- 表单
<form method="post" action="/form" enctype="multipart/form-data">
 <input type="text" name="name"/>
 <input type="file" name="file"/>
 <input type="submit"/>
</form>
- 控制器
@RequestMapping(path = "/form", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("name") String name, 
 @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        byte[] bytes = file.getBytes();
        // store the bytes somewhere
        return "redirect:uploadSuccess";
    }
    return "redirect:uploadFailure";
}
异常处理
先来说下常见的异常处理有几种方式,如下图:

Spring的处理器异常解析 HandlerExceptionResolver 接口的实现负责处理各类控制器执行过程中出现的异常。也是上面提到的,是 DispatcherServlet 中的特殊bean,可以自定义配置处理。
某种程度上讲,HandlerExceptionResolver与你在web应用描述符web.xml文件中能定义的异常映射(exception mapping)很相像,不过它比后者提供了更灵活的方式。比如它能提供异常被抛出时正在执行的是哪个处理器这样的信息。
- HandlerExceptionResolver 提供resolveException接口
public interface HandlerExceptionResolver {
    ModelAndView resolveException(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);
}
- 在BaseController中使用 @ExceptionHandler注解处理异常
@ExceptionHandler(Exception.class)
public Object exceptionHandler(Exception ex, HttpServletResponse response,
 HttpServletRequest request) throws IOException {
        String url = "";
        String msg = ex.getMessage();
        Object resultModel = null;        try {            if (ex.getClass() == HttpRequestMethodNotSupportedException.class) {
                url = "admin/common/500";
                System.out.println("--------毛有找到对应方法---------");
            } else if (ex.getClass() == ParameterException.class) {//自定义的异常
            } else if (ex.getClass() == UnauthorizedException.class) {
                url = "admin/common/unauth";
                System.out.println("--------毛有权限---------");
            }
            String header = req.getHeader("X-Requested-With");
            boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header);
            String method = req.getMethod();
            boolean isPost = "POST".equalsIgnoreCase(method);            if (isAjax || isPost) {                return Message.error(msg);
            } else {
                ModelAndView view = new ModelAndView(url);
                view.addObject("error", msg);
                view.addObject("class", ex.getClass());
                view.addObject("method", request.getRequestURI());                return view;
            }
        } catch (Exception exception) {
            logger.error(exception.getMessage(), exception);            return resultModel;
        } finally {
            logger.error(msg, ex);
            ex.printStackTrace();
        }
    }
- 在web.xml中处理异常
<!-- 默认的错误处理页面 -->
<error-page>
   <error-code>403</error-code>
   <location>/403.html</location>
</error-page>
<error-page>
   <error-code>404</error-code>
   <location>/404.html</location>
</error-page>
<!-- 仅仅在调试的时候注视掉,在正式部署的时候不能注释 --><!-- 这样配置也是可以的,表示发生500错误的时候,转到500.jsp页面处理。 -->
<error-page>
   <error-code>500</error-code>
   <location>/500.html</location>
</error-page>
<!-- 这样的配置表示如果jsp页面或者servlet发生java.lang.Exception类型(当然包含子类)的异常就会转到500.jsp页面处理。 -->
<error-page>
  <exception-type>java.lang.Exception</exception-type>
  <location>/500.jsp</location>
</error-page>
<error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/500.jsp</location>
</error-page>
<!-- 当error-code和exception-type都配置时,exception-type配置的页面优先级高及出现500错误,发生异常Exception时会跳转到500.jsp-->
- 来一个问题:HandlerExceptionResolver和web.xml中配置的error-page会有冲突吗?
解答:如果resolveException返回了ModelAndView,会优先根据返回值中的页面来显示。不过,resolveException可以返回null,此时则展示web.xml中的error-page的500状态码配置的页面。
当web.xml中有相应的error-page配置,则可以在实现resolveException方法时返回null。
API文档中对返回值的解释:
return a corresponding ModelAndView to forward to, or null for default processing.
思维导图让 Spring MVC 不再难懂的更多相关文章
- Mybatis-plus 思维导图,让 Mybatis-plus 不再难懂
		摘要: Mybatis-Plus(简称MP)是一个Mybatis的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. mybatis-plus与mybatis myba ... 
- mybatis-plus思维导图,让mybatis-plus不再难懂
		mybatis-plus与mybatis mybatis Mybatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置 ... 
- java基础思维导图,让java不再难懂
		java基础思维导图,让java不再难懂 原文链接 https://my.oschina.net/u/3080373/blog/873056 最近看了一些文章的思维导图,发现思维导图真是个强大的工具 ... 
- 思维导图与Spring Batch
		最近在学画图,又在复习Spring Batch.不解释,直接上图. 第三章,用XMind画的. 第五章,用iMindMap画的. 
- Spring思维导图,让spring不再难懂(一)
		写在前面 很多人在微信公众号中给我留言说想看spring的思维导图,正好也打算写.与其他框架相比,spring项目拥有更多的模块,我们常用的ioc,mvc,aop等,这些是spring的主要板块.一篇 ... 
- Spring思维导图(MVC篇)
		写在前面 生活就像海洋,只有意志坚强的人才能到达彼岸. 已经很久没有发文章了呀,想必大家都挂念我了,哈哈. 温故而知新,今天一起来复习一下spring mvc的内容吧. spring mvc简介与运行 ... 
- Spring思维导图(AOP篇)
		什么是aop AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP允许 ... 
- Spring思维导图(IOC篇)
		写在前面 写过java的都知道:所有的对象都必须创建:或者说:使用对象之前必须先创建.而使用ioc之后,你就可以不再手动创建对象,而是从ioc容器中直接获取对象. 就好像我们无需考虑对象的销毁回收一样 ... 
- Spring思维导图(一)
		关于Spring Spring是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架 ... 
随机推荐
- Codeforces761B Dasha and friends                                                                                            2017-02-05 23:34             162人阅读              评论(0)              收藏
			B. Dasha and friends time limit per test 2 seconds memory limit per test 256 megabytes input standar ... 
- ZSTU4266  回文                                                                                            2017-03-22 14:25             55人阅读              评论(0)              收藏
			4266: 回文 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1636 Solved: 504 Description 小王想知道一个字符串是否为 ... 
- 浏览器缓存和Service Worker
			浏览器缓存和Service Worker @billshooting 2018-05-06 字数 6175 Follow me on Github 标签: BOM 1. 传统的HTTP浏览器缓存策略 ... 
- TL-WN725N v2.0 树莓派驱动
			TL-WN725N 1.0版Pi是可以直接识别的,但是后来TL-WN725N又出了个2.0版的,要用第三方驱动. 安装步骤如下: wget https://dl.dropboxusercontent. ... 
- WPF 使用QRCoder生成二维码
			vs中使用Nuget获取QRCoder 窗体中添加按钮和Iage <Window x:Class="QRCoderTest.MainWindow" xmlns="h ... 
- JavaSe    之三目运算符应用
			刚学习Java基本知识,对于三目运算符的应用,尝试了做出求某年份中月份的天数. /*自己随意定义一个年份和月份,使用三目运算符 取得当前月份具体天数.并且输出在控制台. 注意哦:大月31天,小月30天 ... 
- python中的MRO和C3算法
			一. 经典类和新式类 1.python多继承 在继承关系中,python子类自动用友父类中除了私有属性外的其他所有内容.python支持多继承.一个类可以拥有多个父类 2.python2和python ... 
- Spark Streaming初步使用以及工作原理详解
			在大数据的各种框架中,hadoop无疑是大数据的主流,但是随着电商企业的发展,hadoop只适用于一些离线数据的处理,无法应对一些实时数据的处理分析,我们需要一些实时计算框架来分析数据.因此出现了很多 ... 
- 【OCP认证12c题库】CUUG 071题库考试原题及答案(28)
			28.choose the best answer Evaluate the following SQL statement: SQL> SELECT promo_id, promo_categ ... 
- 【OCP认证12c题库】CUUG 071题库考试原题及答案(25)
			25. choose the best answer Evaluate the following SQL statement: ALTER TABLE hr.emp SET UNUSED (mgr_ ... 
