mvc:resources
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
15.12.4 mvc:resources
This tag allows static resource requests following a particular URL pattern to be served by a ResourceHttpRequestHandler
from any of a list of Resource
locations. This provides a convenient way to serve static resources from locations other than the web application root, including locations on the classpath. The cache-period
property may be used to set far future expiration headers (1 year is the recommendation of optimization tools such as Page Speed and YSlow) so that they will be more efficiently utilized by the client. The handler also properly evaluates the Last-Modified
header (if present) so that a 304
status code will be returned as appropriate, avoiding unnecessary overhead for resources that are already cached by the client. For example, to serve resource requests with a URL pattern of /resources/**
from a public-resources
directory within the web application root, the tag would be used as follows:
()
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
To serve these resources with a 1-year future expiration to ensure maximum use of the browser cache and a reduction in HTTP requests made by the browser:
<mvc:resources mapping="/resources/**" location="/public-resources/" cache-period="31556926"/>
The mapping
attribute must be an Ant pattern that can be used by SimpleUrlHandlerMapping
, and the location
attribute must specify one or more valid resource directory locations. Multiple resource locations may be specified using a comma-seperated list of values. The locations specified will be checked in the specified order for the presence of the resource for any given request. For example, to enable the serving of resources from both the web application root and from a known path of /META-INF/public-web-resources/
in any jar on the classpath, the tag would be specified as:
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/public-web-resources/"/>
When serving resources that may change when a new version of the application is deployed, it is recommended that you incorporate a version string into the mapping pattern used to request the resources, so that you may force clients to request the newly deployed version of your application's resources. Such a version string can be parameterized and accessed using SpEL so that it may be easily managed in a single place when deploying new versions.
As an example, let's consider an application that uses a performance-optimized custom build (as recommended) of the Dojo JavaScript library in production, and that the build is generally deployed within the web application at a path of /public-resources/dojo/dojo.js
. Since different parts of Dojo may be incorporated into the custom build for each new version of the application, the client web browsers need to be forced to re-download that custom-built dojo.js
resource any time a new version of the application is deployed. A simple way to achieve this would be to manage the version of the application in a properties file, such as:
application.version=1.0.0
and then to make the properties file's values accessible to SpEL as a bean using the util:properties
tag:
<util:properties id="applicationProps" location="/WEB-INF/spring/application.properties"/>
With the application version now accessible via SpEL, we can incorporate this into the use of the resources
tag:
<mvc:resources mapping="/resources-#{applicationProps['application.version']}/**" location="/public-resources/"/>
and finally, to request the resource with the proper URL, we can take advantage of the Spring JSP tags:
<spring:eval expression="@applicationProps['application.version']" var="applicationVersion"/> <spring:url value="/resources-{applicationVersion}" var="resourceUrl">
<spring:param name="applicationVersion" value="${applicationVersion}"/>
</spring:url> <script src="${resourceUrl}/dojo/dojo.js" type="text/javascript"> </script>
mvc:resources的更多相关文章
- 增加mvc:resources后访问不了注解配置的controller的问题
刚开始没有配置mvc:resourcescontroller能够正确访问,但是由于web.xml使用/拦截了所有的请求,所以静态资源访问不上增加mvc:resources之后,静态资源是能访问上了,但 ...
- Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> 转载
Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> mvcmvc:resources ...
- 关于spring 3.0.5的 <mvc:resources mapping="***" location="***">标签的使用
spring mvc 的<mvc;resources mapping="***" location="***">标签是在spring3.0.4出现的 ...
- 关于SpringMVC中找不到<mvc:resources/>标签的解决办法
在springMVC中我们经常会用到<mvc:resources/>标签,但是有些编辑器中的schema过于陈旧.导致找不到<mvc:resources/>标签. 经过试验,有 ...
- SpringMVC同时使用<mvc:resources … />和日期转换Formatter时出现问题的解决方法
很久没更新博文了,不是没有学习,而是很多东西记在OneNote里面,收获很多啊,因为各种杂事,所以对于博客很久没更新了. 个人觉得:博客比起OneNote或者为知笔记之类的云笔不同在于博客应该记载的是 ...
- Info模式下的隐形杀手(SpringMVC同时使用<mvc:resources.../>和FormattingConversionServiceFactoryBean时出现的问题)
天气一天比一天变的凉快了,而我一天天踩的坑更加贱了,首先在北京向各位问好,也给身边献身教育事业的亲朋好友们补上一句节日快乐! 今天早上手贱把项目误删了,不得不去SVN上去乞求了.我个人习惯项目运行的时 ...
- spring-mvc里的 <mvc:resources> 及静态资源访问
在进行Spring MVC的配置时,通常我们会配置一个dispatcher servlet用于处理对应的URL.配置如下: <servlet> <servlet-name>Sp ...
- <mvc:resources mapping="/xxx/**" location="/xxx/"/>无效,可能和Controller的URL模式有关
某项目webapp下有子目录res,其中有img.css.js等存放静态资源的文件夹. 在定义了dispacher-servlet的<url-pattern>/</url-patte ...
- Spring MVC ,使用mvc:resources标签后,处理器无法被访问
在SpringMVC的配置文件中添加了<mvc:resources mapping="/img/**" location="/img/"/>以便处理 ...
随机推荐
- HDU 3600 Simple Puzzle 归并排序 N*N数码问题
先介绍八数码问题: 我们首先从经典的八数码问题入手,即对于八数码问题的任意一个排列是否有解?有解的条件是什么? 我在网上搜了半天,找到一个十分简洁的结论.八数码问题原始状态如下: 1 2 3 4 5 ...
- 第50课 C++对象模型分析(上)
1. 回归本质 (1)class是一种特殊的结构体 ①在内存中class依旧可以看作变量的集合 ②class与struct遵循相同的内存对齐规则 ③class中的成员函数与成员变量是分开存放的.即每个 ...
- Javascript中的对象和原型(3)
在Javascript中的对象和原型(二)中我们提到,用构造函数创建的对象里面,每个对象之间都是独立的,这样就会降低系统资源的利用率,解决这样问题,我们就要用到下面提到的原型对象. 一 原型对象 原型 ...
- javascript获取当前的时间戳
JavaScript 获取当前时间戳:第一种方法: var timestamp = Date.parse(new Date()); 结果:1280977330000第二种方法: var timesta ...
- JavaScript---Angular 和JQuery
Angular 三层模式 M model和data 数据层 , V view 视图层 , C controller 控制器,程序主逻辑 ,通过指令扩展HTML,通过表达式绑定数据到HTML. Vi ...
- mac终端命令大全介绍
OSX 的文件系统 OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 US ...
- 入门Linux
45分钟带你入门Linux(附:笔者在工作室开讨论班录制的视频讲解) 第一部分 熟悉Linux基本操作 一.初识Linux 1.Linux特点 ◊ 开放性 ◊ 多用户 ◊ 多任务 ◊ ...
- 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法
今天遇到了这个问题, 电脑系统:vs2010 win7系统 iis7 我运行在iis中配置的那个网站后,报错:错误代码 0x800700b7 配置错误定义了重复的“system.web.extensi ...
- Linux第十一次学习笔记
异常控制流 异常控制流(ECF)发生在计算机系统的各个层次 在硬件层,硬件检测到的事件会触发控制突然转移到异常处理程序. 在操作系统层,内核通过上下文转换将控制从一个用户进程转移到另一个用户进程. 在 ...
- 【开源】XPShadow, 用阴影让UWP更有层次感
UWP采用的是纯扁平化的设计,个人感觉极端了点,整个世界都是平的,导致App分不清层次,看不出重点.其实扁平化是趋势,android, ios都在搞,问题是android, ios都可以很轻松的实现阴 ...