1、STS-> File -> New Spring Starter Project 自動聯(lián)網(wǎng)
Service URL : http://start.spring.io
Name:SBWeb
Type : maven Package:Jar
Java Version: 8 Language: Java
Group : org.wl
ArtiFact:SBWeb
Description : webproject...
Package: org.boot.wl
2、場景選擇器
Avaliable: web 搜索
選中Web模塊下的Web
3而芥、在org.boot.wl下創(chuàng)建controller包
@Controller
public class BootController{
@ResponseBody
@RequestMapping("/request")
public String request(){
return "hello spring boot!";
}
}
4、spring boot是一個jar,因此靜態(tài)資源就不是再存放在webapps中,存放在哪里?
靜態(tài)資源的存放路徑由WebMvcAutoConfiguration類中-addResourceHandlers()指定:/webjars/
以前引入js等靜態(tài)資源萌抵,是將這些資源下載并手工放入到webapp目錄中,而spring boot將這些靜態(tài)資源直接以jar文件的形式引入項目
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-1</version>
</dependency>
spring boot將靜態(tài)資源存入到j(luò)ar包中,引入:從Jar目錄結(jié)構(gòu)的webjars開始寫:http://localhost:8080/webjars/jquery/3.3.1-1/jquery.js
如何自己寫靜態(tài)資源绍填,如何放到如spring boot中霎桅?
spring boot約定:spring boot將一些目錄結(jié)構(gòu)設(shè)置成靜態(tài)資源存放目錄。
在ResourceProperties類中的CLASSPATH_RESOURCE_LOCATIONS中設(shè)置:
classpath: /META-INF/resources/, /resources/,/static/, /public/
classpath就對應(yīng)src/main/resources目錄
訪問的時候不需要寫這些資源路徑的,比如: http://localhost:8080/hello.html
5讨永、設(shè)置歡迎頁
WebMvcAutoConfiguration類中的welcomePageHandlerMapping() --> getIndexHtml() -> location +"index.html"滔驶,即任意一個靜態(tài)資源目錄中的index.html
6、網(wǎng)站中網(wǎng)頁標(biāo)簽的logo是固定名字:favicon.ico
自定義favicon.ico : 只需要將favicon.ico文件放入任意靜態(tài)資源目錄中即可卿闹。
總結(jié):1.通過源碼發(fā)現(xiàn)靜態(tài)資源的目錄
2.用靜態(tài)資源:只需要將靜態(tài)資源放入以上目錄即可
3.其他特定的文件(歡迎頁瓜浸、ico)只需要根據(jù)約定默認(rèn)名放入資源目錄即可
約定優(yōu)于配置
配置靜態(tài)資源:
spring.resources.static-locations=classpath:/res/,"classpath:/img/"
但是自定義之后就不能使用約定的目錄