如何通过注释在spring mvc 3中设置header no cache [英] how to set header no cache in spring mvc 3 by annotation
问题描述
如何通过注释在spring mvc 3中设置header no cache?不是
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);
推荐答案
没有这样的选择。您可以使用拦截器:
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>
(取自这里)
一方面,没有这样的注释是合乎逻辑的。 spring-mvc方法的注释主要是让容器决定调用哪个方法(通过请求头,请求url或方法来限制它)。控制响应不属于这一类。
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.
另一方面 - 是的,拥有它们会很方便,因为当控制器经过单元测试时,它不是与测试http头文件有关(或者是吗?)。还有 @ResponseBody
和 @ResponseStatus
,它们确实指定了一些响应属性。
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.
这篇关于如何通过注释在spring mvc 3中设置header no cache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!