近期的一個web作業,想把它上傳到服務器。之前服務器放html都是直接放文件,沒有路由配置什麼的,這次用spring boot打包了一下。文件結構是醬紫的
網上有各種解決spring boot訪問靜態頁面、訪問css和js資源的方法,照着配是基本沒有問題的。然鵝,我這裏有個外層文件夾,導致一些奇奇怪怪的問題發生。首先是Controller,我是醬紫寫的
@RequestMapping("/cumt_110/index")
public String indexHtml(HashMap<String, Object> map) {
return "/cumt_110/index.html";
}
@RequestMapping("/cumt_110/introduce")
public String introHtml(HashMap<String, Object> map) {
return "/cumt_110/intro.html";
}
似乎沒有什麼問題,本地服務器訪問ip:prot/cumt_index是可以訪問到的。在頁面跳轉中,首頁跳轉到介紹頁面我原本是醬紫寫的
<li class="nav-item">
<a class="nav-link menu-style" href="/cumt_110/introduce">學校簡介 </a>
</li>
然後從index點擊跳轉到introduce頁面的時候,報404了,一看地址欄多了一個cumt_110???好吧,把href改一下
<li class="nav-item">
<a class="nav-link menu-style" href="introduce">學校簡介 </a>
</li>
這樣就可以了。然鵝,正當我開開心心打包jar包上傳服務器運行,訪問報錯Whitelabel Error Page 500???然後開始嘗試各種網上方法,好像沒有什麼用啊。似乎不是少了什麼東西的原因,我跟網上教程最大的不同就是文件外面還有個文件夾,我就開始考慮是不是文件路徑的問題。最後找出來問題是在Controller的return裏多了一個/。。。。絕了。改成下面醬紫就好了
@RequestMapping("/cumt_110/index")
public String indexHtml(HashMap<String, Object> map) {
return "cumt_110/index.html";
}
@RequestMapping("/cumt_110/introduce")
public String introHtml(HashMap<String, Object> map) {
return "cumt_110/intro.html";
}
因爲cumt_110這個文件夾就是templates文件夾下的,然後我又把classpath寫出了/templates/,那麼就不用在cumt_110前加/了。但是加了/在本地是可以讀出來的,放到服務器就不行,就很奇怪。
spring.thymeleaf.prefix=classpath:/templates/