springboot控制静态资源的缓存

说明

前后端分离的项目请绕道,本文只针对springboot + thymeleaf的项目,使用md5策略。
以下两种方法可以同时使用

方法一

写一个全局的@ControllerAdvice控制类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

@ControllerAdvice
public class ControllerConfig {

    @Autowired
    private ResourceUrlProvider resourceUrlProvider;

    @ModelAttribute("urls")
    public ResourceUrlProvider urls() {
        return this.resourceUrlProvider;
    }
}

thymeleal引入静态资源文件${urls.getForLookupPath(‘资源文件路径’)}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" th:href="${urls.getForLookupPath('/css/init.css')}">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#aa').text('你好');
        });
    </script>
</head>
<body>
    <div th:text="${success}"></div>
    <div id="aa"></div>
</body>
</html>

测试代码

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping(value = "success")
    public String success(ModelMap map) {
        map.put("success", "成功");
        return "success";
    }
}

访问http://localhost:8080/success
页面显示
在这里插入图片描述
查看网页源代码
在这里插入图片描述

方法二 (更简单)

写一个@Configuration配置类,加一个ResourceUrlEncodingFilter ,本人比较懒,直接写在了启动类中。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;

@SpringBootApplication
public class HelloworldMainApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloworldMainApplication.class, args);
    }
    
    @Bean
    public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
    	return new ResourceUrlEncodingFilter();
    }
}

thymeleal引入静态资源文件@{资源文件路径}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" th:href="@{/css/init.css}">
    <script type="text/javascript" src="webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#aa').text('欢迎');
        });
    </script>
</head>
<body>
    <div th:text="${success1}"></div>
    <div id="aa"></div>
</body>
</html>

测试代码

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    
    @RequestMapping(value = "success1")
    public String success1(ModelMap map) {
        map.put("success1", "成功");
        return "success1";
    }
}

访问http://localhost:8080/success1
页面显示
在这里插入图片描述
查看网页源代码
在这里插入图片描述

完成

普通ssm + jsp项目后期加对静态资源缓存控制的方法请看
记一次项目后期对静态资源的处理

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值