zoukankan      html  css  js  c++  java
  • 《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中添加如下代码

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>

    注意:上方只处理*.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/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/mvc
            http://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该类

  • 相关阅读:
    Handler使用总结(转)
    LR连接oracle数据库-lr_db_connect
    selenium2(WebDriver)环境搭建
    使用selenium控制滚动条(非整屏body)
    selenium-打开IE浏览器遇到问题记录
    使用re-sign.jar对apk进行重签名
    Robotium-无源码测试
    genymotion不能联网
    SQL 常用脚本
    todolist
  • 原文地址:https://www.cnblogs.com/xinhudong/p/8323857.html
Copyright © 2011-2022 走看看