javascript
《SpringMVC从入门到放肆》三、DispatcherServlet的url-pattern配置详解
上一篇我們?cè)敿?xì)解釋了一下SrpingMVC的執(zhí)行流程以及一些默認(rèn)的配置,在Spring的思想中,就是默認(rèn)大于配置。今天我們來(lái)詳細(xì)的研究一下DispatcherServlet的url-pattern配置。
一、DispatcherServlet的url-pattern配置
在沒(méi)有特別要求的情況下,SpringMVC的中央調(diào)度器DispatcherServlet的url-pattern常使用后綴匹配方式進(jìn)行配置,如*.do、*.action
注意:這里的url-pattern不能寫/*,因?yàn)镈ispatcherServlet會(huì)將向JSP的動(dòng)態(tài)頁(yè)面跳轉(zhuǎn)請(qǐng)求也當(dāng)作為普通的Controller來(lái)處理。中央調(diào)度器在調(diào)用處理器映射器來(lái)為其查找相應(yīng)的處理器時(shí),肯定找不到。所以在這種情況下,所有的JSP頁(yè)面跳轉(zhuǎn)都會(huì)變?yōu)?04。
最好也不要寫成/,因?yàn)镈ispatcherServlet會(huì)將向靜態(tài)資源的請(qǐng)求當(dāng)作為普通的Controller來(lái)處理。如.css、.jpg、.js等。所以靜態(tài)資源也會(huì)變成404。
所以建議寫成*.do、*.action之類的配置。當(dāng)然也有一些時(shí)候不得不配置成/,當(dāng)開(kāi)發(fā)一些移動(dòng)端接口采用restful請(qǐng)求時(shí),需要配置成/。
二、url-pattern配置為/時(shí)靜態(tài)資源的訪問(wèn)
1:使用tomcat的默認(rèn)Servlet解決
在web.xml中添加如下代碼
注意:上方只處理*.js,如果需要大家可以再加幾個(gè)攔截其它資源。使用該配置只需要配置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對(duì)所有的web應(yīng)用程序生效,專門處理靜態(tài)資源。(處理所有沒(méi)有匹配到servlet mappings的請(qǐng)求)
2:使用SpringMVC的default-servlet-handler解決
在springmvc.xml中添加<mvc:default-servlet-handler/>。當(dāng)然添加這個(gè)default-servlet-handler時(shí),需要對(duì)當(dāng)前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/><!-- 注冊(cè)視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 注冊(cè)SpringMVC處理器 --><bean id="/my.do" class="cn.wechatbao.controller.MyController"></bean> </beans>注意:default-servlet-handler會(huì)對(duì)靜態(tài)資源的訪問(wèn)請(qǐng)求通過(guò)handlerMapping映射到默認(rèn)的Servlet請(qǐng)求處理器DefaultServletHttpRequestHandler類上。而該類最終調(diào)用的是Tomcat的defaultServlet來(lái)處理的請(qǐng)求。如圖:
3:使用SpringMVC的resources解決
在springmvc.xml中添加如下代碼:
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
其中的location和mapping為具體的靜態(tài)資源文件夾,大家可以根據(jù)具體的項(xiàng)目來(lái)定義。
注意:該方法是在spring3.0.4版本后,專門定義的一個(gè)靜態(tài)資源的處理器ResourceHttpRequestHandler類,該種配置文件會(huì)將所有的靜態(tài)資源映射到ResourceHttpRequestHandler該類
轉(zhuǎn)載于:https://www.cnblogs.com/xinhudong/p/8323857.html
總結(jié)
以上是生活随笔為你收集整理的《SpringMVC从入门到放肆》三、DispatcherServlet的url-pattern配置详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [Swift]LeetCode1118.
- 下一篇: 轻轻松松看懂Spring AOP源码