Spring 中的静态资源无法正常工作。有任何想法吗?

Static resources in Spring not working. Any ideas?

情况:

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

我也创造了 project/src/main/resources/templates/index.html(它是百里香) 需要 a.js by <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 参数

有什么想法吗?

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

同时删除您的 MvcConfig,因为它不是必需的。