服务端设置JS版本号解决浏览器缓存问题(Maven打包自动设置)

2 篇文章 0 订阅

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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值