Skip to content

Commit 68d4a70

Browse files
committed
Add option to always append 'must-revalidate'
Issue: SPR-9248
1 parent 24ffa5a commit 68d4a70

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,9 @@
2626
import org.springframework.util.StringUtils;
2727
import org.springframework.web.HttpRequestMethodNotSupportedException;
2828
import org.springframework.web.HttpSessionRequiredException;
29+
import org.springframework.web.context.request.WebRequest;
2930
import org.springframework.web.context.support.WebApplicationObjectSupport;
31+
import org.springframework.web.servlet.mvc.LastModified;
3032

3133
/**
3234
* Convenient superclass for any kind of web content generator,
@@ -79,6 +81,8 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
7981

8082
private int cacheSeconds = -1;
8183

84+
private boolean alwaysMustRevalidate = false;
85+
8286

8387
/**
8488
* Create a new WebContentGenerator which supports
@@ -194,6 +198,25 @@ public final boolean isUseCacheControlNoStore() {
194198
return this.useCacheControlNoStore;
195199
}
196200

201+
/**
202+
* An option to add 'must-revalidate' to every Cache-Control header. This
203+
* may be useful with annotated controller methods, which can
204+
* programmatically do a lastModified calculation as described in
205+
* {@link WebRequest#checkNotModified(long)}. Default is "false",
206+
* effectively relying on whether the handler implements
207+
* {@link LastModified} or not.
208+
*/
209+
public void setAlwaysMustRevalidate(boolean mustRevalidate) {
210+
this.alwaysMustRevalidate = mustRevalidate;
211+
}
212+
213+
/**
214+
* Return whether 'must-revaliate' is added to every Cache-Control header.
215+
*/
216+
public boolean isAlwaysMustRevalidate() {
217+
return alwaysMustRevalidate;
218+
}
219+
197220
/**
198221
* Cache content for the given number of seconds. Default is -1,
199222
* indicating no generation of cache-related headers.
@@ -313,7 +336,7 @@ protected final void cacheForSeconds(HttpServletResponse response, int seconds,
313336
if (this.useCacheControlHeader) {
314337
// HTTP 1.1 header
315338
String headerValue = "max-age=" + seconds;
316-
if (mustRevalidate) {
339+
if (mustRevalidate || this.alwaysMustRevalidate) {
317340
headerValue += ", must-revalidate";
318341
}
319342
response.setHeader(HEADER_CACHE_CONTROL, headerValue);

0 commit comments

Comments
 (0)