Springboot 集成Thymeleaf 引入js失效 解决方法

一、问题描述

今天在github上下载了一个项目,项目结构Springboot + thymeleaf,在使用过程中发现前端页面按钮点击后事件无响应,在F12调试后查看后发现问题所在:
在这里插入图片描述
在F12调试后发现是相应的js文件全是404,为什么会找不到引入的js文件呢?以下是原HTML中js文件的引入写法

	/*这里是HTML引入js文件的代码*/
	<script type="text/javascript" th:src="@{~/js/aes.js}"></script>
    <script type="text/javascript" th:src="@{~/js/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{~/js/security.js}"></script>
    <script type="text/javascript" th:src="@{~/js/jquery-2.2.3.min.js}"></script>
    <script type="text/javascript" th:src="@{~/js/random.js}"></script>

后续我将引入js文件的方式做了以下改变:

	/********第一次更改********//********无效  依旧404********/
	<script type="text/javascript" th:src="@{./js/aes.js}"></script>
    <script type="text/javascript" th:src="@{./js/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{./js/security.js}"></script>
    <script type="text/javascript" th:src="@{./js/jquery-2.2.3.min.js}"></script>
    <script type="text/javascript" th:src="@{./js/random.js}"></script>

	/********第二次更改********//********无效  依旧404********/
	<script type="text/javascript" th:src="@{../static/js/aes.js}"></script>
    <script type="text/javascript" th:src="@{../static/js/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{../static/js/security.js}"></script>
    <script type="text/javascript" th:src="@{../static/js/jquery-2.2.3.min.js}"></script>
    <script type="text/javascript" th:src="@{../static/js/random.js}"></script>

在经过两次更改js文件引入方式后,都没能解决404的问题,接下来我把矛头指向了Springboot,怀疑是Springboot在资源转发的过程中进行了拦截。

二、解决办法

在转变思想后,给Springboot添加了静态资源配置:

@Controller
@SpringBootApplication
public class CustomeWebConfiguration extends WebMvcConfigurationSupport {

    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
    }
}

结果解决了js文件引入404问题:
在这里插入图片描述

  • 可用:
	<script type="text/javascript" th:src="@{./js/aes.js}"></script>
    <script type="text/javascript" th:src="@{./js/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{./js/security.js}"></script>
    <script type="text/javascript" th:src="@{./js/jquery-2.2.3.min.js}"></script>
    <script type="text/javascript" th:src="@{./js/random.js}"></script>
  • 可用:
	<script type="text/javascript" th:src="@{~/js/aes.js}"></script>
    <script type="text/javascript" th:src="@{~/js/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{~/js/security.js}"></script>
    <script type="text/javascript" th:src="@{~/js/jquery-2.2.3.min.js}"></script>
    <script type="text/javascript" th:src="@{~/js/random.js}"></script>

注意:使用…/static/**写法引入js文件,需要将Springboot静态资源配置更改为以下方式:

@Controller
@SpringBootApplication
public class CustomeWebConfiguration extends WebMvcConfigurationSupport {

    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/");
    }
}
  • 这样引入的js文件也可用:
	<script type="text/javascript" th:src="@{../static/js/aes.js}"></script>
    <script type="text/javascript" th:src="@{../static/js/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{../static/js/security.js}"></script>
    <script type="text/javascript" th:src="@{../static/js/jquery-2.2.3.min.js}"></script>
    <script type="text/javascript" th:src="@{../static/js/random.js}"></script>

在这里插入图片描述


给文章点个赞吧~!

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhuzicc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值