1.springmvc 接受請求訪問的流程如下所示:
即:
DispatcherServlet是前置控制器,配置在web.xml文件中的。攔截匹配的請求,Servlet攔截匹配規則要自己定義,把攔截下來的請求,依據相應的規則分發到目標Controller來處理,是配置spring MVC的第一步。 但是,我們在搭建SpringMVC框架時,往往總是把DispatcherServlet的配置文件放錯位置。網上有的說:springDispatcherServletMVC-servlet.xml(暫且命名這么一個springDispatcherServletMVC的dispatchservlet配置文件)應該放在WEB-INF下面(與web.xml放一起);有的說:應該直接把springDispatcherServletMVC-servlet.xml放在src文件夾下面;有的說:在java resources下面新建一個config的resource folder,把配置文件都放在這個資源文件夾下面。那么到底哪種是正確的呢? 答案是:這個要根據web.xml中DispatcherServlet的配置聲明有關系。
關於url-pattern的配置 url-pattern配置有三種: 1.*.do 訪問以.do結尾的由DispatcherServlet進行解析. /(斜杠) 所有訪問的地址都由DispatcherServlet進行解析,對於靜態的文件解析需要配置,不讓DispatcherServlet進行解析. 注意:使用此種方式可以實現 RESTful風格的url. /* 這樣配置不對,使用這種配置,最終要轉發到一個jsp頁面時,仍然會由DispatcherServlet進行解析,但是不能根據這個jsp頁面找到handler所以會報錯. 注意:當你配置了Spring MVC,同樣還是需要在web.xml中配置ContextLoaderListener監聽器的,雖然Spring MVC是Spring的一個模塊,可以做到無縫整合,但是他們的配置是獨立的.
springmvc的配置文件一般都是在web.xml里面指定位置的。其實,springmvc的配置文件有默認位置。因此,存在兩種配置方式:
(1)采用指定位置配置方式,即在web.xml配置文件中指定springmvc配置文件的位置。
文件結構如下:


<!-- springmvc 前端控制器 --> <servlet> <servlet-name>dispatcherSerlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherSerlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
上面代碼中,明確了contextConfigLocation的地址為:classpath:(即在classess文件夾下面),而且命名為springmvc.xml(或者其他名稱都可以),指定配置文件位置時,這個配置文件的名字可以隨便取。
(2)采用默認位置配置方式
當web.xml中DispatcherServlet配置聲明中,沒有明確DispatcherServlet前端控制器配置文件的位置時,則系統默認DispatcherServlet前端控制器配置文件放在WEB-INF文件夾下。
文檔結構如下:
<!-- Spring MVC 的Servlet,它將加載WEB-INF/springDispatcherServlet-servlet.xml 的配置文件,以啟動Spring MVC模塊--> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
上面代碼聲明了一個命名為springDispatcherServlet的前端控制器(DispatcherServlet),並且沒有指定該servlet配置文件的路徑,那么系統將以默認名字springDispatcherServlet-servlet.xml在默認路徑/WEB-INF下尋找它,位置不正確,名字不正確,都會報錯。(這種命名是規定好的,前端控制器的名字-servlet這種形式。)
下面是spring-servlet.xml的配置
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:websocket="http://www.springframework.org/schema/websocket" 7 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 11 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 12 http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"> 13 <!-- <mvc:annotation-driven /> 會自動注冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean,是spring MVC為@Controllers分發請求所必須的。它提供了數據綁定支持,讀取json的支持 --> 14 <mvc:annotation-driven /> 15 16 <!-- 設置自動注入bean的掃描范圍,use-default-filters默認為true,會掃描所有的java類進行注入 ,--> 17 <!-- Use-dafault-filters=”false”的情況下:<context:exclude-filter>指定的不掃描,<context:include-filter>指定的掃描 --> 18 <!-- springmvc和application文件都需要配置,但mvc文件只掃描controller類,application掃描不是controller類 --> 19 <context:component-scan base-package="mytest.*" use-default-filters="false"> 20 <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 21 </context:component-scan> 22 23 <!-- 文件上傳功能需該配置 --> 24 <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"> 25 <property name="defaultEncoding" value="UTF-8"/> 26 </bean> 27 28 <!-- ResourceBundleThemeSource是ThemeSource接口默認實現類--> 29 <bean class="org.springframework.ui.context.support.ResourceBundleThemeSource" id="themeSource"/> 30 31 <!-- 用於實現用戶所選的主題,以Cookie的方式存放在客戶端的機器上--> 32 <bean class="org.springframework.web.servlet.theme.CookieThemeResolver" id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard"/> 33 34 <!-- 由於web.xml文件中進行了請求攔截 35 <servlet-mapping> 36 <servlet-name>dispatcher</servlet-name> 37 <url-pattern>/</url-pattern> 38 </servlet-mapping> 39 這樣會影響到靜態資源文件的獲取,mvc:resources的作用是幫你分類完成獲取靜態資源的責任 40 --> 41 <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" /> 42 43 <!-- 配置使用 SimpleMappingExceptionResolver 來映射異常 --> 44 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > 45 46 <!-- 定義默認的異常處理頁面 --> 47 <property name="defaultErrorView" value="error"/> 48 <!-- 配置異常的屬性值為ex,那么在錯誤頁面中可以通過 ${exception} 來獲取異常的信息如果不配置這個屬性,它的默認值為exception--> 49 <property name="exceptionAttribute" value="exception"></property> 50 <property name="exceptionMappings"> 51 <props> 52 <!-- 映射特殊異常對應error.jsp這個頁面 --> 53 <prop key=".DataAccessException">error</prop> 54 <prop key=".NoSuchRequestHandlingMethodException">error</prop> 55 <prop key=".TypeMismatchException">error</prop> 56 <prop key=".MissingServletRequestParameterException">error</prop> 57 </props> 58 </property> 59 </bean> 60 61 <!-- 配置jsp視圖解析器 --> 62 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver"> 63 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 64 <property name="prefix" value=""/> 65 <property name="suffix" value=".jsp"/> 66 </bean> 67 </beans>
參考鏈接:
https://blog.csdn.net/qq_36324685/article/details/79928766
https://www.jianshu.com/p/9575e95a4eda
https://www.jianshu.com/p/6587555a7123
https://blog.csdn.net/zwl18210851801/article/details/78489021
http://www.cnblogs.com/ioufev/p/9950768.html
https://blog.csdn.net/sinat_25318461/article/details/60962122
https://www.cnblogs.com/Jason-Xiang/p/6544188.html