java web项目发版js或css缓存问题解决方案
给引入的js或css文件加版本号
如
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
// 加载资源文件
PropertiesResourceFileUtils prfu = new PropertiesResourceFileUtils("version.properties");
%>
<script type="text/javascript" src="<%=basePath%>script/common/common.js?version=<%=prfu.getValue("version") %>"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/epoch.css?version=<%=prfu.getValue("version") %>">
version.properties文件:
#版本号
version=20161125.02
public class PropertiesResourceFileUtils {
/**
* Properties对象
*/
private Properties properties = new Properties();
/**
* PropertiesResourceFileUtils构造函数
* @param propertiesPath 文件路径
*/
public PropertiesResourceFileUtils(String propertiesPath) {
try {
InputStream in = PropertiesResourceFileUtils.class.getClassLoader().getResourceAsStream(propertiesPath);
if(in != null) {
properties.load(in);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* 通过KEY获取值
* @param key
* @return
*/
public String getValue(String key){
String value = properties.getProperty(key);
if(StringUtils.isNotBlank(value))
try {
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
}