Spring MVC 4中<mvc:resources>配置cache-period不生效问题排查
<mvc:resources> Cache Header Issue Hey there, let's figure out why your expected my-age header isn't showing up, and fix the problem step by step.
First, a Key Clarification
The cache-period attribute in <mvc:resources> doesn't generate a custom my-age header. Instead, it sets the max-age value in the standard Cache-Control response header. So if you're looking for my-age, that's why it's missing—this configuration was never meant to create that header. But if you do need my-age, we'll cover how to add it later.
Troubleshooting Steps for Cache Header Issues
Verify Spring MVC Setup
Make sure your XML configuration includes<mvc:annotation-driven/>. The<mvc:resources>element relies on Spring MVC's resource handling infrastructure, which is enabled by this tag. Without it, your resource configuration might not be processed at all.Check Request Path Matching
Confirm the resource you're requesting actually matches the/resources/**mapping. For example, if you're accessinghttp://your-app/css/style.cssinstead ofhttp://your-app/resources/css/style.css, the<mvc:resources>rule won't apply, and no cache headers will be set.Check for Conflicting Interceptors/Filters
Other interceptors or filters (like security filters, logging filters) in your application might be overriding or removing response headers. Review your configuration to see if any components modify response headers for resource requests.Test with Raw HTTP Requests
Bypass browser caching to get accurate results. Use tools likecurlto send a request and inspect the headers directly:curl -I http://your-app/resources/your-resource.jsLook for the
Cache-Controlheader—if yourcache-periodis working, it should look likeCache-Control: max-age=259201, public(depending on your setup).
How to Add the Custom my-age Header
If your requirement is specifically to return a my-age header (not just the standard Cache-Control), here are two ways to do it:
Option 1: Use <mvc:header> Inside <mvc:resources>
Extend your existing configuration to include the custom header:
<mvc:resources mapping="/resources/**" location="/resources/" cache-period="259201"> <mvc:header name="my-age" value="259201"/> </mvc:resources>
This will add both the standard Cache-Control header (from cache-period) and your custom my-age header.
Option 2: Create a Custom Interceptor
Write a handler interceptor to inject the header for resource requests:
public class CustomAgeHeaderInterceptor implements HandlerInterceptor { @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // Apply only to resource requests if (request.getRequestURI().startsWith("/resources/")) { response.setHeader("my-age", "259201"); } } }
Then register the interceptor in your XML configuration:
<mvc:interceptors> <bean class="com.your.package.CustomAgeHeaderInterceptor"/> </mvc:interceptors>
内容的提问来源于stack exchange,提问作者Marcin Warycha

