Ajax获取版本号
前端代码
<script type="text/javascript">
var _version = '1.0.0';
function setVersion(version) {
document.writeln("<script src='/lib/js/index.js?v=" + version + "'><"+'/'+"script>");
document.writeln("<script src='/lib/js/souce.js?v=" + version + "'><"+'/'+"script>");
}
$.ajax({
url: '/system/jsVersion',
type: 'get',
async: false,
success: function (data) {
if (data) {
_version = data;
}
setVersion(_version);
},
error: function () {
setVersion(_version);
}
});
</script>
服务端代码
package com.system.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/system")
public class SystemController {
@Value("${js.version}")
private String jsVersion;
@RequestMapping("jsVersion")
String getJsVersion() {
return jsVersion;
}
}
JSP获取版本号
<%@ page import="java.util.ResourceBundle" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
// properties 配置文件名称(application.properties)
ResourceBundle res = ResourceBundle.getBundle("application");
String version = res.getString("js.version");
%>
<script src="/lib/js/index.js?v=<%=version%>"></script>
<script src="/lib/js/souce.js?v=<%=version%>"></script>
properties文件
js.version=${js.version}
加载配置文件
<context:property-placeholder location="classpath:*.properties" ignore-unresolvable="true"/>
POM文件配置
<properties>
<!-- 设置时间格式 -->
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<!-- 设置properties文件属性值 -->
<js.version>${maven.build.timestamp}</js.version>
</properties>