Java 如何通过注释在spring mvc 3中设置标题无缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4364622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set header no cache in spring mvc 3 by annotation
提问by EdwardLau
how to set header no cache in spring mvc 3 by annotation? not is
如何通过注释在spring mvc 3中设置标题无缓存?不是
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
采纳答案by Bozho
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.
一方面,没有这样的注释是合乎逻辑的。spring-mvc 方法上的注解主要是让容器决定调用哪个方法(通过请求头、请求 url 或方法来限制它)。控制响应不属于这一类。
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.
另一方面 - 是的,拥有这些会很方便,因为当控制器进行单元测试时,它与测试 http 标头内容无关(或者是吗?)。还有@ResponseBody
and @ResponseStatus
,它们确实指定了一些响应属性。
回答by Risadinha
To override the settings for certain controller mappings use the cacheMappings properties object on the WebContentInterceptor
要覆盖某些控制器映射的设置,请使用 WebContentInterceptor 上的 cacheMappings 属性对象
<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>
回答by njfife
I know this is old but this might be helpful to some.
我知道这很旧,但这可能对某些人有所帮助。
If you wanted to add a lot more logic to when you cache and when you don't you can also write a custom interceptor.
如果您想在缓存和不缓存时添加更多逻辑,您还可以编写自定义拦截器。
For example if you wanted to disable caching in the response only when the browser is IE or only from specific urls you can do that as well by extending the HandlerInterceptorinterface.
例如,如果您只想在浏览器是 IE 或仅从特定 url 时禁用响应中的缓存,您也可以通过扩展HandlerInterceptor接口来实现。
By doing that you can have a lot of control over exactly what happens. It's not as easy as just setting the header for everything at once or just typing in the changes to the response in each controller but it is also not that hard and is a better long term solution in my opinion. It is also a good thing to know how to do in spring generally.
通过这样做,您可以对发生的事情有很多控制。这并不像一次为所有内容设置标题或只是在每个控制器中输入对响应的更改那么容易,但它也不那么难,在我看来是一个更好的长期解决方案。一般在春天知道怎么做也是一件好事。
This is a pretty good tutorial for it:
这是一个非常好的教程:
http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/
http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/