springboot的目录文件结构

1目录讲解

  src/main/java:存放代码

  src/main/resources

      static:存放静态文件,比如:css  js image   (访问方式:http://localhost:8080/js/main.js)

      templates:存放静态页面  jsp ,html,tpl

      config : 存放配置文件,例如,application.properties

                 resources:存放脚本

2  pom.xml中引入依赖 Thymeleaf

     <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-thymeleaf</artifactId>

   </dependency>

3文件的加载顺序,先加载静态文件

springboot会默认从 META/recources  > recources>staitic>public      里面是否存在相应的资源,如果有则直接返回,如果一直找不到,就会报错

可以自己在staic 下面多建几个文件,文件加中新建test.js。里面写一个测试js  console.info("xxx")  ,

然后run application  ,

通过地址访问  例如:http://localhost:8080/test.js

如果是访问templates下面的index.html,还是需要通过controller进行映射index.html

上例子:

3.1 在templates下面新建一个index.html的页面

 3.2在pom.xml中添加模板依赖

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-thymeleaf</artifactId>

   </dependency>

3.3在controller文件夹下添加FileController

  

 3.4 运行application,访问地址:http://localhost:8080//api/v1/index

请注意:controller中应该注释掉有相对路径的方法,否则无法访问到对应的 图片

4默认配置

(1)spring官网的默认配置

https://docs.spring.io/springboot/docs/current/reference/html/boot-features-developing-web-application.html#boot-features-spring-mvc-static-content

(2)在src/amin/resources中新建一个test文件夹,在里面有一个test.js,正常的localhost:8080/test/test.js是无法访问的,需要在src/amin/resourcesxia 新建一个application.properties,里面复制

spring.resources.static-location=classpath:/META-INF/resources/,classpath:/recources/,classpath:/static/,classpath:/public/,classpath:/test/

然后重新启动application,使用localhost:8080/test/test.js才能访问到test.js

原文地址:https://www.cnblogs.com/zhushilai/p/13497899.html