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 ResourceHandlerRegistry
but 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
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 ResourceHandlerRegistry
but 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
Reset to default 6That'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.
This is how you call, note that ../.. actually goes to /resources :
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf">
<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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744228606a4564130.html
评论列表(0条)