zoukankan      html  css  js  c++  java
  • 利用 filter 机制 给 静态资源 url 加上时间戳,来防止js和css文件的缓存,利于开发调试

    直接上代码:

    public class WeiXinFilter implements Filter{
    	private static Logger logger = LoggerFactory.getLogger(WeiXinFilter.class);
    	public void init(FilterConfig fConfig) throws ServletException {}
    	public void destroy() {}
    
    	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    		HttpServletRequest req = (HttpServletRequest)request;
    		HttpServletResponse resp = (HttpServletResponse)response;
    		
    		String requestURL = req.getRequestURL().toString();
    		String queryStr = req.getQueryString();
    		
    		// add timestamp to static resource, to avoid cache
    		if(requestURL != null && (requestURL.endsWith(".js") || requestURL.endsWith(".css"))){	// static resource
    			String newURL = null;
    			if(StringUtils.isNotBlank(queryStr) && queryStr.trim().indexOf(ParameterConfig.STATIC_TAIL) == -1){
    				newURL = requestURL + "?" + queryStr + "&" + ParameterConfig.STATIC_TAIL + new Date().getTime();
    				resp.sendRedirect(newURL);
    				return;
    			}
    			if(StringUtils.isBlank(queryStr)){
    				newURL = requestURL + "?" + ParameterConfig.STATIC_TAIL + new Date().getTime();
    				resp.sendRedirect(newURL);
    				return;
    			}
    			
    			try{
    				chain.doFilter(request, response);
    			}catch(Exception e){
    				logger.error(e.toString());
    			}
    			return;
    		}
    
    public class ParameterConfig 
    {
    	
    	/** 静态资源 为防止缓存,加上时间戳标志 */
    	public static final String STATIC_TAIL = "__oawx_t=";
    

      

    配置下过滤器就行了,chrome 调试的 效果如下,可以看到  chrome浏览器在获取css和js文件时,路径上携带了时间戳:

    在开发阶段还是比较有用的。

  • 相关阅读:
    记录一次SpringCloud Alibaba整合Springboot出现的'com.netflix.client.config.IClientConfig' that could not be found
    ysoserial-URLDNS链分析
    DIVA闯关-APP测试
    前端页面直接转换为PDF文件流
    中位数最大问题
    【vim】Linux添加环境变量等
    FFmpeg使用笔记
    【memo】及时留坑
    【album】深度学习 / 机器学习 / 人工智能
    【Linux】软件安装使用【aubio / FFmpeg】
  • 原文地址:https://www.cnblogs.com/digdeep/p/5539131.html
Copyright © 2011-2022 走看看