javascript
《SpringMVC从入门到放肆》三、DispatcherServlet的url-pattern配置详解
上一篇我們詳細解釋了一下SrpingMVC的執行流程以及一些默認的配置,在Spring的思想中,就是默認大于配置。今天我們來詳細的研究一下DispatcherServlet的url-pattern配置。
一、DispatcherServlet的url-pattern配置
在沒有特別要求的情況下,SpringMVC的中央調度器DispatcherServlet的url-pattern常使用后綴匹配方式進行配置,如*.do、*.action
注意:這里的url-pattern不能寫/*,因為DispatcherServlet會將向JSP的動態頁面跳轉請求也當作為普通的Controller來處理。中央調度器在調用處理器映射器來為其查找相應的處理器時,肯定找不到。所以在這種情況下,所有的JSP頁面跳轉都會變為404。
最好也不要寫成/,因為DispatcherServlet會將向靜態資源的請求當作為普通的Controller來處理。如.css、.jpg、.js等。所以靜態資源也會變成404。
所以建議寫成*.do、*.action之類的配置。當然也有一些時候不得不配置成/,當開發一些移動端接口采用restful請求時,需要配置成/。
二、url-pattern配置為/時靜態資源的訪問
1:使用tomcat的默認Servlet解決
在web.xml中添加如下代碼
注意:上方只處理*.js,如果需要大家可以再加幾個攔截其它資源。使用該配置只需要配置servlet-mapping即可,default的Servlet配置在tomcat的conf/web.xml文件中。如下圖:
具體的解釋在該段代碼的上方注釋里。
<!-- The default servlet for all web applications, that serves static --> <!-- resources. It processes all requests that are not mapped to other --> <!-- servlets with servlet mappings (defined either here or in your own --> <!-- web.xml file). This servlet supports the following initialization --> <!-- parameters (default values are in square brackets): -->該default的servlet對所有的web應用程序生效,專門處理靜態資源。(處理所有沒有匹配到servlet mappings的請求)
2:使用SpringMVC的default-servlet-handler解決
在springmvc.xml中添加<mvc:default-servlet-handler/>。當然添加這個default-servlet-handler時,需要對當前xml添加mvc的約束xsd。如下圖:
最終springmvc.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><mvc:default-servlet-handler/><!-- 注冊視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 注冊SpringMVC處理器 --><bean id="/my.do" class="cn.wechatbao.controller.MyController"></bean> </beans>注意:default-servlet-handler會對靜態資源的訪問請求通過handlerMapping映射到默認的Servlet請求處理器DefaultServletHttpRequestHandler類上。而該類最終調用的是Tomcat的defaultServlet來處理的請求。如圖:
3:使用SpringMVC的resources解決
在springmvc.xml中添加如下代碼:
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
其中的location和mapping為具體的靜態資源文件夾,大家可以根據具體的項目來定義。
注意:該方法是在spring3.0.4版本后,專門定義的一個靜態資源的處理器ResourceHttpRequestHandler類,該種配置文件會將所有的靜態資源映射到ResourceHttpRequestHandler該類
轉載于:https://www.cnblogs.com/xinhudong/p/8323857.html
總結
以上是生活随笔為你收集整理的《SpringMVC从入门到放肆》三、DispatcherServlet的url-pattern配置详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Swift]LeetCode1118.
- 下一篇: leetcode-92-反转链表②