1

I am trying to create a Spring Boot with Mvc and AngularJs. I modified the View Resolver of my app so I can point my application to look on the "WEB-INF/pages/" folder for my static html files. Now on the html file I declared the source of my AngularJs libraries by using the webjars dependency

<script src="webjars/angularjs/1.5.5/angular.min.js"></script>

I know that in order to make this work is I have to declare the classpath of the webjars libray on the application's ResourceHandlerRegistry so on my configuration file I added only this line of code

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}

After adding this line my code somehow works but what makes me curious is that about this line on my html file

<script src="resources/js/angular/app.js"></script>

I noticed that on a Spring MVC application that in order for Spring to detect the Javascript files under the webapp's resource folder is that we have to declare that folder on the ResourceHandlerRegistry just like this

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

but I haven't declared my webapp's resource folder on my Spring Boot's ResourceHandlerRegistrybut still the application can see my app.js file and my AngularJs codes still works. Here is the file structure of my Application

src |--main |--java |--resources |--webapp |--resources |--js |--angular |--app.js |--WEB-INF |--index.html

1 Answer 1

6

That's how spring boot Web's static works. By default, you should put your js,image,css in the resources/static. If you are using thymeleaf, put the template in the resources. Sample as below.

enter image description here

This is how you call, note that ../.. actually goes to /resources :

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"
    href="../../css/bootstrap.min.css" />
    <link rel="stylesheet" th:href="@{/css/signin.css}"
    href="../../css/signin.css" />
</head>

UPDATE after OP gave the file structure : the resources located at webapp/resources which is default static folder for spring mvc ( not spring boot - refer to this blog ).

The spring boot's static folders are located at (refer to Spring IO Blog ) :

  • /META_INF/resources
  • /static
  • /resources
  • /public
4
  • I edited my question for clarification, I did not use thymleaf for my application and created a webapp folder. The resources folder I referring to is the one under the webapp folder not the resources folder under the src/main/resources
    – anathema
    Commented Jun 2, 2016 at 2:52
  • hemm.. that is weird. The default static folders known to Spring boot are /META-INF/resources/, /resources , /static, and /public ... Refer to spring.io/blog/2013/12/19/…
    – Rudy
    Commented Jun 2, 2016 at 3:07
  • I found the answer, the webapp/resources is spring mvc default static folder, and not spring boot's. Refer to : mkyong.com/spring-mvc/…
    – Rudy
    Commented Jun 2, 2016 at 3:10
  • @anathema have you resolve your issue? If yes, please accept the answer. :)
    – Rudy
    Commented Jul 18, 2017 at 2:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.