springBoot項目中的static和templates文件夾的使用
SpringBoot里面沒有我們之前常規(guī)web開發(fā)的WebContent(WebApp),它只有src目錄在src/main/resources下面有兩個文件夾
static和templates springboot默認 static中放靜態(tài)頁面,而templates中放動態(tài)頁面。但是webapp文件夾可以有,需要配置視圖解析器同樣可以訪問。
1、static文件中(static文件默認是放靜態(tài)資源的)
//這樣寫不能訪問static中index文件夾下的index.html頁面
@RequestMapping("index")
public String hello() {
return "/index/index";
}
//這樣寫才能訪問到
@RequestMapping("index")
public String hello() {
return "/index/index.html";
}
2、templates文件夾
放視圖模板,就是springBoot的動態(tài)頁面,需要引入thymeleaf組件
3、springBoot項目中Controller層的頁面重定向問題,需要引入thymeleaf組件
@RequestMapping("index")
public String hello() {
return "/index/index.html";
}
//請求test會重定向到index
@RequestMapping("test")
public String test() {
return "redirect:/index";
}
當然,也可以轉發(fā),放ip:port/index,打開static下的index目錄中的index.html
@RequestMapping("index")
public String hello() {
return "forward:/index/index.html";
}
4、templates文件夾中的頁面中引入static文件中的資源
編譯之后static文件夾下的文件和templates文件夾下的文件其實會出現在同一個目錄下,引入樣式或者默認圖片的時候注意
到此這篇關于springBoot項目中的static和templates文件夾的使用的文章就介紹到這了,更多相關springBoot static和templates文件夾內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot中使用FastJson解決long類型在js中失去精度的問題
這篇文章主要介紹了springboot中使用FastJson解決long類型在js中失去精度的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringCloud及Nacos服務注冊IP選擇問題解決方法
這篇文章主要介紹了SpringCloud及Nacos服務注冊IP選擇問題,為什么注冊的IP和真實IP不符合呢,原因是Nacos客戶端在注冊服務時會從機器網卡中選擇一個IP來注冊,所以,當注冊了的是非真實IP后,另一臺機器調用時是不可能調通的,知道問題原因就是解決方法,一起看看吧2024-01-01
聊聊springmvc中controller的方法的參數注解方式
本篇文章主要介紹了聊聊springmvc中controller的方法的參數注解方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10

