---
title: "SpringMVC对静态资源进行缓存"
author: "accphc"
date: "2014-05-08T11:31:16Z"
tags: []
categories: ["企业架构"]
slug: "springmvc"
draft: false
lastmod: "2014-05-08T11:31:16Z"
id: 2063660
url: "https://hc24.iteye.com/blog/2063660"
---

在dispatcher-servlet.xml中配置：

```
<!-- 处理静态资源 -->
	<!-- 上传的图片缓存1个月，其他js，css，img资源缓存一年 -->
	<mvc:resources mapping="/res/**" location="/res/" cache-period="2592000"/> 
	<mvc:resources mapping="/resources/**" location="/resources/" cache-period="31536000"/> 
	<mvc:resources mapping="/css/**" location="/css/" cache-period="31536000"/>
	<mvc:resources mapping="/js/**" location="/js/" cache-period="31536000"/>
	<mvc:resources mapping="/img/**" location="/img/" cache-period="31536000"/>
	<mvc:resources mapping="/images/**" location="/images/" cache-period="31536000"/>
```

 

这样配置后，SpringMVC会自动给静态资源Response添加缓存头Cache-Control和Expires值，如下图所示：

![](http://dl2.iteye.com/upload/attachment/0096/8842/ff692b21-b091-39c5-9c8a-307c2e2e215d.png)  
 

请求状态为403，不会再次从服务器得到图片，利用了浏览器缓存，节约请求的流量和请求时间。