Situation:
I have a fresh spring project from start.spring.io
And I have created the file project/src/main/resources/js/a.js
I have created also project/src/main/resources/templates/index.html (it's with thymeleaf)
that needs that a.js by <script th:src="@{/js/a.js}"></script>
I also have the following configuration:
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/js/**")
.addResourceLocations("/js/");
}
}
When the app is starting I can see in the logs:
Mapped URL path [/js/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Tried:
- At the
.addResourceLocations();I have tried all kind of variants like:"/resources/js","resources/js","/js","js" - Also in
application.propertiesusedspring.resources.static-locations=classpath:/resources/ - create
/resources/static/js/a.jsand update theaddResourceLocatiosparameter accordingly
Any ideas?
You will need to place
a.jsfile inside static folder. I have attached folder structure below:Also remove your
MvcConfigsince it is not required.