how to set header no cache in spring mvc 3 by annotation? not is
response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0);
Cache-control: no-cache The no-cache directive uses the ETag header field for validation of the cached response by making a roundtrip to and from the server to ensure that the response has not changed.
The must-revalidate response directive indicates that the response can be stored in caches and can be reused while fresh. If the response becomes stale, it must be validated with the origin server before reuse. Typically, must-revalidate is used with max-age . Cache-Control: max-age=604800, must-revalidate.
Cache-Control: PrivateThe private response directive indicates that a resource is user specific—it can still be cached, but only on a client device. For example, a web page response marked as private can be cached by a desktop browser, but not a content delivery network (CDN).
public class CacheControl extends Object. A builder for creating "Cache-Control" HTTP response headers. Adding Cache-Control directives to HTTP responses can significantly improve the client experience when interacting with a web application.
There is no such option. You can use an interceptor:
<mvc:annotation-driven/> <mvc:interceptors> <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor"> <property name="cacheSeconds" value="0"/> <property name="useExpiresHeader" value="true"/> <property name="useCacheControlHeader" value="true"/> <property name="useCacheControlNoStore" value="true"/> </bean> </mvc:interceptors>
(taken from here)
On one hand it is logical not to have such annotation. Annotations on spring-mvc methods are primarily to let the container decide which method to invoke (limiting it by a request header, request url, or method). Controlling the response does not fall into this category.
On the other hand - yes, it will be handy to have these, because when controllers are unit-tested it is not relevant to test http header stuff (or is it?). And there are @ResponseBody
and @ResponseStatus
, which do specify some response properties.
To override the settings for certain controller mappings use the cacheMappings properties object on the WebContentInterceptor
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor"> <property name="cacheSeconds" value="2100" /> <property name="useExpiresHeader" value="true" /> <property name="useCacheControlHeader" value="true" /> <property name="useCacheControlNoStore" value="true" /> <property name="cacheMappings"> <props> <prop key="/myUncachedController">0</prop> </props> </property>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With