DispatcherServlet的做用

  1. 文件上傳解析,若是請求類型是multipart,將經過MultipartResolver進行文件上傳解析java

  2. 經過HandlerMapping將請求映射處處理器Controller上,(返回一個HandlerExecutionChain,它包括一個處理器、多個HandlerInterceptor攔截器)web

  3. 經過ViewResolver解析邏輯視圖名到具體視圖實現spring

  4. 本地化解析app

  5. 渲染具體的視圖等post

  6. 若是執行過程當中遇到異常將交給HandlerExceptionResolver來解析。編碼

    在web.xml裏面的配置url


  7.     <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!-- load-on-startup:表示啓動容器時初始化該Servlet -->
        <!-- /能夠攔截全部請求 -->

    針對7進行說明:spa

        web容器默認會在/WEB-INF/下面找dispatcherServlet-servler.xml沒有找到會拋異常code

嚴重: Servlet /spring02 threw load() exception
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:140)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)

修改路徑xml

<servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring02-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

web.xml中完整配置以及說明

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <!-- get方式提交這裏的CharacterEncodingFilter沒法處理中文亂碼狀況 -->
    <!-- 這裏的CharacterEncodingFilter只能處理post編碼設置 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring02-servlet.xml</param-value>
            <!-- 有關controller層的配置信息會放到spring02-servlet.xml裏面去 -->
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- load-on-startup:表示啓動容器時初始化該Servlet -->
    <!-- /能夠攔截全部請求 -->

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
        <!-- 通常會將service層,dao層的配置放到applicationContext.xml裏面去 -->
    </context-param>

</web-app>

看圖說話

相關文章
相關標籤/搜索