如果我们使用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. Newtonsoft.Json 序列 反序列 IEnumerable

    下面是memcached 中获取的obj 类型的数据,转list string sessionId = Request.Cookies["sessionId"].Value;//授 ...

  2. JDK安装配置教程

    一.首先下载JDK的最新版本.可以去http://java.sun.com/javase/downloads/index.jsp下载最新版本JDK1.6.一切下载后选择安装路径,例如我选择安装在&qu ...

  3. linux安装-----源码安装步骤--zlib软件安装

    该zlib 可以对许多其他软件的编译代码起着优化 压缩作用. 解压压缩包: .tar.gz------------->tar zxvf 压缩包.tar.gz .tar.bz2---------- ...

  4. DP问题如何确定状态

    DP问题如何确定状态 一.dp实质 动态规划的实质就是通过小规模的同类型的问题来解决题目的问题. 所以有一个dp数组来储存所有小规模问题的解. 所以确定状态也就是缩小问题规模. 我们求解问题的一般规律 ...

  5. 不能在Python Console中运行pytest

    在Python Console中运行pytest发现报错了 这是为什么?因为Python Console已经是进入python之后的环境,就像在python自带的IDLE中运行pytest pytes ...

  6. lightoj1213推公式

    很容易推出来的公式ans=n^(k-1)*k*sum 然后快速幂就好了 #include<map> #include<set> #include<cmath> #i ...

  7. python扫描proxy并获取可用代理ip列表

    mac或linux下可以work的代码如下: # coding=utf-8 import requests import re from bs4 import BeautifulSoup as bs ...

  8. 转: OLAP引擎——Kylin介绍

    本文转自:http://blog.csdn.net/yu616568/article/details/48103415 ,如有侵犯,立刻删除.  Kylin是ebay开发的一套OLAP系统,与Mond ...

  9. LeetCode OJ:Two Sum(两数之和)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  10. ubuntu 交叉编译arm linux 内核小例子

    下载arm-linux-gcc 4.2.3http://code.google.com/p/princess-alist/downloads/detail?name=arm-linux-gcc-4.3 ...