zoukankan      html  css  js  c++  java
  • thyemleaf:禁用JS缓存(原创)

    在开发时经常需要调整JS,但是调整后由于页面缓存的原因,看不到实时效果。

    开发环境:springboot+thymeleaf

    1.配置文件多模式

    image

    image

    2.获得当前的激活的模式和随机数

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Component;
    
    /**
     * 
     * 类描述: 项目主配置文件
     *
     */
    @Component(value = "app")
    @ConfigurationProperties(prefix = "app")
    @PropertySource("classpath:/config/app.properties")
    public class PropertiesApp {
    
      @Autowired
      private Environment env;
    
      private String staticURL;
      private String publicURL;
      private String appURL;
    
      public final String getStaticURL() {
        return staticURL;
      }
    
      public final void setStaticURL(String staticURL) {
        this.staticURL = staticURL;
      }
    
      public final String getPublicURL() {
        return publicURL;
      }
    
      public final void setPublicURL(String publicURL) {
        this.publicURL = publicURL;
      }
    
      public final String getAppURL() {
        return appURL;
      }
    
      public final void setAppURL(String appURL) {
        this.appURL = appURL;
      }
    
      /**
       * 获得:随机数,用作禁用页面缓存
       *
       * @return the Rint
       */
      public final long getRnd() {
        return System.currentTimeMillis();
      }
    
      /**
       * 获得激活的配置文件属性
       * 
       * @return
       */
      public String getActive() {
        return env.getProperty("spring.profiles.active");
      }
    
    }

    3.在页面中判断模式和随机数

    <div th:fragment="js-index">
        <th:block th:switch="${@app.getActive()}">
            <script th:case="'prod'" type="text/javascript" th:src="(${@app.getAppURL()})+'js/index.js'"></script>
            <script th:case="'dev'" type="text/javascript" th:src="(${@app.getAppURL()})+'js/index.js?rnd='+(${@app.getRnd()})"></script>
        </th:block>
    </div>

    ${@app.getActive()} 获得当前的激活模式

    ${@app.getRnd()} 获得随机数

    生成的HTML:

    image

    这样每次加载的JS都是最新的,记住对业务JS实施,不要对公共的JS去做。

  • 相关阅读:
    对象属性编辑器中实现像Size,Rectangle等可以展开的属性
    远程办公产品风口会不会把SOHO自由职业吹起来
    项目加
    推荐几款免费又好用的项目管理工具
    Sprint Retrospective
    敏捷管理的大概背景和Scrum的特性
    推荐几款最好用的项目管理系统,即好用又免费
    项目管理的需求变更问题
    敏捷管理有一个原则就是:拥抱变化
    推荐5款体验最好的项目管理工具
  • 原文地址:https://www.cnblogs.com/huiy/p/9001783.html
Copyright © 2011-2022 走看看