Spring Boot directory file structure

1. explain directory

src / main / java: storing codes

src/main/resources

  resources: (Spring Boot default) to store resource files
  static: (Spring Boot default) to store static files, such as css, js, image, (access method HTTP: // localhost: 8080 / JS / main.js)
  public :( Spring Boot default) to store common files

  templates :( defined by the user, can easily named, but here using a recognized file name) to store static pages, such as JSP, HTML, tpl
  config :( defined by the user, you can easily named, but here using a recognized file name) to store configuration files, such as application.properties

 

 

2. With a loading sequence files, static resource files

Spring Boot default one by one resources> static> public inside to find whether the corresponding resource exists from META / resources>, if there is a direct return.

For example: We are in these folders to create a new js file, it will look at which file to load.

Address bar enter the following address: HTTP: // localhost: 8080 / test.js , here we do not have the resources to build test.js file folder, it will be to load static files folder below

As shown in the figure, we created a new folder templates, which created a new index.html file, because the templates are not Spring Boot the default folder, so we can not access the files in this folder, even if other folder is not the same file name .

要想访问,需要引入依赖(注意:如果不引人这个依赖包,html 文件应该放在默认加载文件夹里面,比如 resources、static、public 这个几个文件夹,才可以访问)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

 写一个测试映射

@RequestMapping(value = "/api/v1/gopage")  
public Object index() {
  return "index";
}

输出效果

 

Spring Boot 给我们定义了一些默认资源文件夹及其加载顺序,但如果用户新增了一些自定义文件夹或者是想改变资源文件的加载顺序,该怎么办呢?

我们可以在根目录下,新建一个 application.properties。

  

可以自己设置加载顺序,也可以添加自己新增的文件夹,如果用户自己新增 application.properties,则Spring Boot会去读这个文件来决定默认文件夹及其加载顺序

spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 

 

官网说明:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content

 

Guess you like

Origin www.cnblogs.com/jwen1994/p/11184152.html