如何通过注释在spring mvc 3中设置标题无缓存 [英] how to set header no cache in spring mvc 3 by annotation

查看:28
本文介绍了如何通过注释在spring mvc 3中设置标题无缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过注解在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中设置标题无缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆