javascript - Spring Boot Static Resources - Stack Overflow

I am trying to create a Spring Boot with Mvc and AngularJs. I modified the View Resolver of my app so I

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

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

Share Improve this question edited Jun 2, 2016 at 3:01 Rudy 7,04412 gold badges53 silver badges87 bronze badges asked Jun 2, 2016 at 2:33 anathemaanathema 9772 gold badges16 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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.

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

相关推荐

  • javascript - Spring Boot Static Resources - Stack Overflow

    I am trying to create a Spring Boot with Mvc and AngularJs. I modified the View Resolver of my app so I

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信