1

情况:

我有一个来自 start.spring.io 的新春季项目
并且我已经创建了文件project/src/main/resources/js/a.js

我还创建了project/src/main/resources/templates/index.html(它与百里香一起)需要a.js<script th:src="@{/js/a.js}"></script>

我也有以下配置:

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/js/**")
                .addResourceLocations("/js/");

    }
}  

当应用程序启动时,我可以在日志中看到:
Mapped URL path [/js/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

试过:

  1. .addResourceLocations();我尝试了各种变体,例如: "/resources/js", "resources/js", "/js","js"
  2. 也在application.properties使用中spring.resources.static-locations=classpath:/resources/
  3. 相应地创建/resources/static/js/a.js和更新addResourceLocatios参数

有任何想法吗?

4

1 回答 1

1

您需要将a.js文件放在静态文件夹中。我在下面附上了文件夹结构:

在此处输入图像描述

也删除你的MvcConfig,因为它不是必需的。

于 2017-12-07T05:41:41.717 回答