SpringBoot目錄文件結構總結(5)


1、目錄

  src/main/java :存放java代碼

  src/main/resources

    static:存放靜態文件,比如css、js、image(訪問方式 http://localhost:8080/js/main.js)

    templates:存放靜態頁面jsp,html,tpl

    config:存放配置文件application.properties

    resources:

  

2、靜態文件加載順序:Spring Boot默認依次從:META/resources > resources >static >public > 如果存在訪問的資源則返回,否則報錯!

3、引入依賴 Thymeleaf

  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>

  新建controller映射過去,例如404錯誤頁面

package cn.xiaobing.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FailController {
    
    @RequestMapping(value = "/v1/fail")
    public Object fail() {        
        return "404";
    }
}

啟動項目:

 訪問映射地址:

 4、static:存放靜態文件,比如css、js、image(訪問方式 http://localhost:8080/image/500.png)

 啟動項目:

 5、自定義路徑訪問如

 新建配置文件:application.properties

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

啟動項目:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

訪問資源地址:

 

6、更多學習請spring官網查詢

7、不足之處,后續補充!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM