在我的Java(UI和业务实现之间的中间件)项目中添加以下代码以在UI中停止缓存。
Java代码:
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies
答案 0 :(得分:0)
通常,您会编写一个过滤器来设置这些标题。
public class NoCacheFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
res.setDateHeader("Expires", -1); // Proxies.
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
并将其添加到您的web.xml
<filter>
<filter-name>NoCacheFilter </filter-name>
<filter-class>com.example.NoCacheFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>NoCacheFilter </filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
我已将过滤器设置为rest
网址上下文。这假设REST服务位于单个上下文中。