(Turn) SpringMVC (1) _Spring mind map, so that Spring is no longer difficult to understand (mvc chapter)

         Foreword: First, outline the overview of SpringMVC in the form of a mind map, and then analyze its use in detail.

This article focuses on the following issues:

  • Introduction and operation principle of spring mvc
  • main annotation
  • ContextLoaderListener
  • DispatcherServlet
  • Tags in applicationContext.xml
  • File Upload
  • exception handling
    Reprinted from: http://www.jianshu.com/p/7c4bbf1ba998

1. Introduction and operating principle of spring mvc

      Spring's Model-View-Controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers and supports configurable handler mapping, view rendering, localization, time zone and theme rendering And so on, it even supports file uploads.

 

  • (1) Http request: The client request is submitted to DispatcherServlet.
  • (2) Find the handler: The DispatcherServlet controller queries one or more HandlerMappings to find the Controller that handles the request.
  • (3) Invoke the processor: DispatcherServlet submits the request to the Controller.
  • (4) (5) Call business processing and return results: After the Controller calls business logic processing, it returns to ModelAndView.
  • (6) (7) Process the view mapping and return the model: DispatcherServlet queries one or more ViewResoler view resolvers to find the view specified by ModelAndView.
  • (8) Http response: The view is responsible for displaying the result to the client.

 2. Main Notes

3.ContextLoaderListener 

       Before talking about ContextLoaderListener, let's first understand the role of web.xml.

  • There can be no web.xml file in a web, that is to say, the web.xml file is not necessary for a web project. The web.xml file is used to initialize configuration information: such as Welcome page, servlet, servlet-mapping, filter, listener, startup loading level, etc. When your web project does not use these, you can configure your Application without the web.xml file.
  • When a web project is to be started, the server software or container such as (tomcat) will load the web.xml file in the project in the first step, and start the project through various configurations. project to start correctly. web.xml has a number of tags, the order in which they are loaded is: context-param >> listener >> fileter >> servlet​ . (Multiple nodes of the same type are loaded in order of appearance)

         The spring mvc startup process is roughly divided into two processes:

  1. The ContextLoaderListener initializes, instantiates the IoC container, and registers this container instance into the ServletContext.
  2. DispatcherServlet initialization.
           The ServletContextListener interface has two methods: contextInitialized and contextDestroyed.
         The configuration of ContextLoaderListener in web.xml is:
<!-- Configure contextConfigLocation initialization parameters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<!-- 配置ContextLoaderListerner -->
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 

4. DispatcherServlet

       Spring MVC框架,与其他很多web的MVC框架一样:请求驱动;所有设计都围绕着一个中央Servlet来展开,它负责把所有请求分发到控制器;同时提供其他web应用开发所需要的功能。不过Spring的中央处理器,DispatcherServlet,能做的比这更多。

       下图展示了Spring Web MVC的DispatcherServlet处理请求的工作流。熟悉设计模式的朋友会发现,DispatcherServlet应用的其实就是一个“前端控制器”的设计模式(其他很多优秀的web框架也都使用了这个设计模式)。

 

 

         在web.xml中的配置:
<!-- servlet定义 -->
<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>
         其中:
  • 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中简单地配置它们。

 

         DispatcherServlet下Bean的详细解释:

        其中,常用的ViewResolver的配置。以jsp作为视图为例:

<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

        配置上传文件限制MultipartResolver:

<!-- 上传限制 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <!-- 上传文件大小限制为31M,31*1024*1024 -->
     <property name="maxUploadSize" value="32505856"/>
</bean>
 

 5. applicationContext.xml中的标签

 6. 文件上传

        前面说到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>
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326271491&siteId=291194637