您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何進(jìn)行springboot配置templates直接訪問(wèn)的實(shí)現(xiàn),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
springboot下的templates目錄的資源默認(rèn)是受保護(hù)的,類似于javaweb項(xiàng)目的WEB-INF目錄,但是給每個(gè)springboot的html頁(yè)面都配置控制器跳轉(zhuǎn)過(guò)于麻煩
在配置文件加如下:
spring.resources.static-locations=classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/templates/, classpath:/public/
附上spring 各種配置的官方url:方便后期查閱
@Controller public class HelloController { @RequestMapping("/test") public String test(Model model){ model.addAttribute("msg","<h2>templates測(cè)試</h2>"); model.addAttribute("users", Arrays.asList("lishao","liyuan")); return "/test"; } }
在html中調(diào)用
<body> <h4>test</h4> <!--不轉(zhuǎn)義--> <div th:text="${msg}"></div> <!--轉(zhuǎn)義h2--> <div th:utext="${msg}"></div> <hr> <h4 th:each="user : ${users}" th:text="${user}"></h4> </body>
記得要導(dǎo)入templates的依賴
當(dāng)你導(dǎo)入了templates依賴,
就會(huì)直接識(shí)別出來(lái)文件下的test,簡(jiǎn)單方便
<!--templates--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
html中也要導(dǎo)入
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
一個(gè)是轉(zhuǎn)義一個(gè)是不轉(zhuǎn)義
以下是運(yùn)行的結(jié)果
關(guān)于如何進(jìn)行springboot配置templates直接訪問(wèn)的實(shí)現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。