在開發時常常須要調整JS,可是調整後因爲頁面緩存的緣由,看不到實時效果。javascript
開發環境:springboot+thymeleafjava
1.配置文件多模式spring
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.在頁面中判斷模式和隨機數 springboot
<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
${@app.getRnd()} 得到隨機數 this
生成的HTML:spa
這樣每次加載的JS都是最新的,記住對業務JS實施,不要對公共的JS去作。code