Spriongboot創(chuàng)建的項(xiàng)目岸军,在resources -> templates下的資源是不能直接訪(fǎng)問(wèn)的,沒(méi)有開(kāi)放訪(fǎng)問(wèn)權(quán)限。這是因?yàn)閠emplates文件夾菩混,是放置模板文件的死相,因此需要視圖解析器來(lái)解析它融求。所以必須通過(guò)服務(wù)器內(nèi)部進(jìn)行訪(fǎng)問(wèn),也就是要走控制器 -> 服務(wù) -> 視圖解析器這個(gè)流程才行算撮。同時(shí)生宛,存在安全問(wèn)題。比如說(shuō)肮柜,你把你后臺(tái)的html文件放到templates陷舅,而這個(gè)文件夾對(duì)外又是開(kāi)放的,就會(huì)存在安全隱患审洞。
這里提供兩種可以方式訪(fǎng)問(wèn)templates模板下的資源文件
方式一:在application.yml或者application.properties配置文件中將訪(fǎng)問(wèn)權(quán)限開(kāi)放(這種方式不推薦)
spring:
resources:
static-locations: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/, classpath:/templates/
或者
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/
方式二:通過(guò)內(nèi)部controller跳轉(zhuǎn)訪(fǎng)問(wèn)的資源
比如要訪(fǎng)問(wèn)templates下的test.html莱睁。先請(qǐng)求內(nèi)部控制器,由控制器跳轉(zhuǎn)test.html頁(yè)面
image.png
@Controller
public class Test {
@GetMapping("/test")
public String test() {
return "test";
}
}