如果我们使用spring mvc来做web访问请求的控制转发,那么默认所有访问都将被DispatcherServlet独裁统治。比如我现在想访问的欢迎页index.html根本无需任何业务逻辑处理,仅仅就展示一句话而已,但spring mvc还是会把访问index.html这件事交给DispatcherServlet处理,而DispatcherServlet会从HandlerMapping去找对应Controller注解,如果找不到就报错了:

No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'service-dispatcher'

  spring mvc提供的解决方案是在配置文件中加入mvc:resources来专门标识静态资源,比如我的web.xml是这样的:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Memcache View Application</display-name> <servlet>
<servlet-name>service-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>service-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-core.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

  那么我的spring-mvc.xml就要是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="com.wulinfeng.memcache.view" />
<mvc:annotation-driven />
<mvc:resources location="/" mapping="/**/*.js"/>
<mvc:resources location="/" mapping="/**/*.css"/>
<mvc:resources location="/" mapping="/**/*.png"/>
<mvc:resources location="/" mapping="/*.html"/>
</beans>

  这时就可以顺利访问index.html这个欢迎页了。其他js、css或png图片的静态资源也可以直接访问。如果我还要做拦截,比如登陆功能,又不想拦截到静态资源,怎么办呢?最简单的就是配置拦截时过滤静态资源:

    <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/**/*.css" />
<mvc:exclude-mapping path="/**/*.js" />
<mvc:exclude-mapping path="/**/*.png" />
<mvc:exclude-mapping path="*.html" />
<mvc:exclude-mapping path="/login**" />
<mvc:exclude-mapping path="/register**" />
<mvc:exclude-mapping path="/getVerifyCode**" />
<mvc:exclude-mapping path="/getMethod/**" />
<bean class="com.wulinfeng.test.testpilling.util.InterceptorUtil" />
</mvc:interceptor>
</mvc:interceptors>

spring mvc静态资源访问的配置的更多相关文章

  1. 7.Spring MVC静态资源访问

    在SpringMVC中常用的就是Controller与View.但是我们常常会需要访问静态资源,如html,js,css,image等. 默认的访问的URL都会被DispatcherServlet所拦 ...

  2. Spring MVC静态资源访问

    最近在学习servlet的时候发现自己不能访问到css和js, 于是google一番学到不少方法加载,总结如下: 1.对于Spring MVC, 由于我们截获了所有请求<url-pattern& ...

  3. spring mvc 静态资源 404问题

    spring mvc 静态资源 404问题 在web.xml配置servlet-mapping的时候,如果url-pattern设置为"/" (如下),很多人都会遇到导入js,cs ...

  4. Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> 转载

    Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> mvcmvc:resources  ...

  5. spring boot 开静态资源访问,配置视图解析器

    配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...

  6. Spring Boot 静态资源访问原理解析

    一.前言 springboot配置静态资源方式是多种多样,接下来我会介绍其中几种方式,并解析一下其中的原理. 二.使用properties属性进行配置 应该说 spring.mvc.static-pa ...

  7. spring mvc静态资源请求和<mvc:annotation-driven>

    自己看了官方文档,也到网上查了下,目前理解如下: <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和Annotat ...

  8. Spring MVC静态资源处理(在applicationContex.xml文件中进行配置)

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  9. spring mvc: 静态资源/文件配置

    静态文件不用再放web-info 下面了,放在webapp/ 下面就行了(静态文件放web-inf下你在jsp都无法引用~  注意一下所有js.css包括报表文件~ 配置文件等等等~  不要放在web ...

随机推荐

  1. c++ learning

    迟到了三年的学习笔记.. 野指针:造了一个指针,不是NULL或者没有指向正经内存.比如刚造出来又不赋值,并不知道它指向了哪里 内存泄漏:造了一个指针,给他分配了空间,xxxxx,又分配了一块空间,指针 ...

  2. Pandas统计函数

    统计方法有助于理解和分析数据的行为.现在我们将学习一些统计函数,可以将这些函数应用到Pandas的对象上. pct_change()函数 系列,DatFrames和Panel都有pct_change( ...

  3. spring mvc: 页面重定向调整

    我的项目名称是hello, 在src/main/java目录下面建了一个chapter2目录 有三个配置文件: web.xml, chapter2-servlet.xml, applicationCo ...

  4. 在UIElement外面多套一层布局面板(Grid、StackPanel)的意义

    在一个UIElement或多个UIElement外面套上一层布局面板(Grid.StackPanel),可以起到统一管理作用(非重点关注):另外,更重要的是:可以起到扩大UIElement操作有效范围 ...

  5. 四边形不等式优化_石子合并问题_C++

    在动态规划中,经常遇到形如下式的状态转移方程: m(i,j)=min{m(i,k-1),m(k,j)}+w(i,j)(i≤k≤j)(min也可以改为max) 上述的m(i,j)表示区间[i,j]上的某 ...

  6. 更新增加一个门店ID字段的值

    MYSQL因为不能查询一张表时同时更新一张表,同时又会有子查询大于等于一条的情况出现. 分两种情况: 1 直接JOIN 得到一张表. 然后导出做筛选 CREATE TABLE TEST SELECT ...

  7. ControllerDescriptor的认识

    ControllerDescriptor类主要包含了对ASP.NET MVC中的Control的元数据的解析,在MVC的Model绑定以及数据处理过程中经常会遇到ControllerDescripto ...

  8. 内存保护机制及绕过方法——利用未启用SafeSEH模块绕过SafeSEH

    利用加载模块之外的地址绕过safeSEH 前言:文章涉及的概念在之前的文章中都有过详细的讲解 ⑴.  原理分析: 当程序加载进内存中后,处理PE文件(exe,dll),还有一些映射文件,safeSEH ...

  9. 用同步的方式执行jQuery异步动画

    在编写jQuery动画时,通过回调函数进行动画队列的编排,但当回调过多,往往会出现这样的代码: $(".box1").fadeIn(1000,function(){ $(" ...

  10. vsftpd的530 Login incorrect错误解决方法 vsftpd登录错误

    530 Login incorrect只有用匿名anonymous才可登录,其余所有用户都报530 Login incorrect错 复制代码 代码如下: local_enable=YESwrite_ ...