Velocity 添加版本号
@(Velocity 添加版本号)[js版本号|velocity配置]
Velocity 添加版本号是为了应对前端js、css缓存的影响,在项目更新的时候用户的浏览器中会缓存之前项目的js和css文件所以在项目更新后,缓存问题会诱发很多问题,而给css和js添加版本号可以解决这个问题。
JAVA代码
VersionTool
public class VersionTool {
private static final Logger LOG = Logger.getLogger(VersionTool.class);
private static String jsVersion;
private static String cssVersion;
// 初始化参数
static {
try {
InputStream in = Constants.class.getClassLoader()
.getResourceAsStream("configure.properties");
Properties pros = new Properties();
pros.load(in);
jsVersion = pros.getProperty("js.version");
cssVersion = pros.getProperty("css.version");
in.close();
} catch (Exception e) {
LOG.error("", e);
}
}
public String getJsVersion() {
return jsVersion;
}
public String getCssVersion() {
return cssVersion;
}
}
配置文件
configure.properties
#js和css的版本控制
js.version=1.0.1
css.version=1.0.1enter code here
XML文件配置
toolbox.xml
<?xml version="1.0" encoding="UTF-8"?>
<tools>
<toolbox scope="application">
<!-- <tool key="math" class="org.apache.velocity.tools.generic.MathTool"/> -->
<!-- 版本控制工具 -->
<tool key="versionTool" class="com.ucmed.common.util.VersionTool" />
</toolbox>
</tools>
Spring配置
<bean
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<!--版本控制-->
<property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>
<property name="cache" value="false" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="viewClass"
value="com.ucmed.common.velocity.VelocityLayoutToolboxView" />
<property name="contentType" value="text/html;charset=UTF-8" />
<!-- <property name="exposeSessionAttributes"> <value>true</value> </property>
<property name="exposeRequestAttributes"> <value>true</value> </property> -->
</bean>