SpringBoot 目錄文件結構及訪問

1. 基本目錄結構


src/main/java: 存放源碼
src/main/resources
    static/: 存放靜態文件,比如css、js、image(訪問方式 http://localhost:8080/js/main.js)
    templetes/: 存放靜態頁面jsp,html,tpl
    config/: 存放配置文件,application.properties
    resources/:
    public/:
    application.properties

2. 靜態資源文件的加載順序


  • 官方文檔地址

  • 靜態資源路徑默認配置:

    spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
    
  • SpringBoot 默認會挨個從 META/resources > resources > static > public 裏面找是否存在相應的資源,如果有則直接返回。

3. 自定義靜態資源文件目錄


  • 修改 src/main/resources/application.properties 文件,覆蓋springboot的默認配置

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

4. 訪問靜態頁面


1. 方法一

  1. 引入依賴

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
  2. 添加controller

    @RequestMapping(value = "/api/v1/gopage")
    public Object index() {
        return "index";
    }
    
    • 此處Controller不能使用RestController註解,只能使用Controller註解

2. 方法二

  • 將靜態頁面放到加載目錄中,就可以直接訪問。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章