《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中添加如下代码

1<servlet-mapping>
2    <servlet-name>default</servlet-name>
3    <url-pattern>*.js</url-pattern>
4</servlet-mapping>

注意:上方只处理*.js,如果需要大家可以再加几个拦截其它资源。使用该配置只需要配置servlet-mapping即可,default的Servlet配置在tomcat的conf/web.xml文件中。如下图:

具体的解释在该段代码的上方注释里。

441889-20180121122619365-1365634217.png

1<!-- The default servlet for all web applications, that serves static -->
2<!-- resources. It processes all requests that are not mapped to other -->
3<!-- servlets with servlet mappings (defined either here or in your own -->
4<!-- web.xml file). This servlet supports the following initialization -->
5<!-- parameters (default values are in square brackets): -->

该default的servlet对所有的web应用程序生效,专门处理静态资源。(处理所有没有匹配到servlet mappings的请求)

2:使用SpringMVC的default-servlet-handler解决
在springmvc.xml中添加。当然添加这个default-servlet-handler时,需要对当前xml添加mvc的约束xsd。如下图:

441889-20180121122650521-1120467435.png

最终springmvc.xml如下:

 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4    xmlns:context="http://www.springframework.org/schema/context"
5    xmlns:aop="http://www.springframework.org/schema/aop"
6    xmlns:tx="http://www.springframework.org/schema/tx"
7    xmlns:mvc="http://www.springframework.org/schema/mvc"
8    xsi:schemaLocation="http://www.springframework.org/schema/beans
9        http://www.springframework.org/schema/beans/spring-beans.xsd
10        http://www.springframework.org/schema/context
11        http://www.springframework.org/schema/context/spring-context.xsd
12        http://www.springframework.org/schema/aop
13        http://www.springframework.org/schema/aop/spring-aop.xsd
14        http://www.springframework.org/schema/tx
15        http://www.springframework.org/schema/tx/spring-tx.xsd
16        http://www.springframework.org/schema/mvc
17        http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>

18
19    <mvc:default-servlet-handler/>
20
21    <!-- 注册视图解析器 -->
22    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
23        <property name="prefix" value="/WEB-INF/jsp/" />
24        <property name="suffix" value=".jsp" />
25    </bean>
26
27    <!-- 注册SpringMVC处理器 -->
28    <bean id="/my.do" class="cn.wechatbao.controller.MyController"></bean>
29</beans>

注意:default-servlet-handler会对静态资源的访问请求通过handlerMapping映射到默认的Servlet请求处理器DefaultServletHttpRequestHandler类上。而该类最终调用的是Tomcat的defaultServlet来处理的请求。如图:

441889-20180121122738724-155163214.png

3:使用SpringMVC的resources解决
在springmvc.xml中添加如下代码:

1<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>

其中的location和mapping为具体的静态资源文件夹,大家可以根据具体的项目来定义。
注意:该方法是在spring3.0.4版本后,专门定义的一个静态资源的处理器ResourceHttpRequestHandler类,该种配置文件会将所有的静态资源映射到ResourceHttpRequestHandler该类.


关于作者: 王俊南(Jonas)

昨夜寒蛩不住鸣。惊回千里梦,已三更。起来独自绕阶行。人悄悄,帘外月胧明。 白首为功名。旧山松竹老,阻归程。欲将心事付瑶琴。知音少,弦断有谁听。

热门文章