日期:2014-05-17  浏览次数:20514 次

JSP中,防止 css js img 等文件的缓存的方法

被这个问题困扰过很多回,上网也查过很多方法,基本上就是在 <head> 标签中添加如下元素:

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  

?

可是,照做了也没用。无意中想到,能不能在css之类的文件后面加上一个随机数或者当前时间什么的呢?试了一下,还真行:

<link rel="stylesheet" href="<%=request.getContextPath()%>/css/endparameter.css?<%= System.currentTimeMillis()%>" />

?

但如果像下面这么写就是不行的, 因为script标签不能被嵌入到 link 中使用吧:

<link rel="stylesheet" href="<%=request.getContextPath()%>/css/endparameter.css?"+ <script type="text/javascript">Math.random()</script> />

?