在SpringBoot中訪問靜態資源

第一種方式 : 放在src/main/webapp目錄下

放在webapp目錄下的靜態資源是可以直接訪問的

這裏寫圖片描述

user.html

這裏寫圖片描述

2.png

這裏寫圖片描述

在user.html中引用2.png

這裏寫圖片描述

第二種方式:放在classpath下

ResourceProperties中的說明

org.springframework.boot.autoconfigure.web.ResourceProperties

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };

靜態資源默認放在classpath路徑下:Defaults to classpath:[/META-INF/resources/,/resources/, /static/, /public/] plus context:/ (the root of the servlet context).

這裏寫圖片描述

person/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link href="/css/main.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/js/main.js"></script>
<script type="text/javascript">

    sayHello();

</script>
</head>
<body>
    <h3>person page HTML</h3>
</body>
</html>

這裏寫圖片描述

通過修改配置項,設置靜態資源的位置

application.properties

# 修改默認的靜態資源存放目錄
spring.resources.static-locations=classpath:/web/

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章