springboot配置templates直接訪問的實現(xiàn)
springboot配置templates直接訪問
springboot下的templates目錄的資源默認是受保護的,類似于javaweb項目的WEB-INF目錄,但是給每個springboot的html頁面都配置控制器跳轉(zhuǎn)過于麻煩
配置公有訪問方式如下
在配置文件加如下:
spring.resources.static-locations=classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/templates/, classpath:/public/
附上spring 各種配置的官方url:方便后期查閱
springboot的templates用法
@Controller
public class HelloController {
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("msg","<h1>templates測試</h1>");
model.addAttribute("users", Arrays.asList("lishao","liyuan"));
return "/test";
}
}
在controller中添加視圖
在html中調(diào)用
<body>
<h3>test</h3>
<!--不轉(zhuǎn)義-->
<div th:text="${msg}"></div>
<!--轉(zhuǎn)義h1-->
<div th:utext="${msg}"></div>
<hr>
<h3 th:each="user : ${users}" th:text="${user}"></h3>
</body>
記得要導(dǎo)入templates的依賴
當你導(dǎo)入了templates依賴,

就會直接識別出來文件下的test,簡單方便
<!--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">
一個是轉(zhuǎn)義一個是不轉(zhuǎn)義
以下是運行的結(jié)果


以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成JmsTemplate(隊列模式和主題模式)及xml和JavaConfig配置詳解
這篇文章主要介紹了SpringBoot集成JmsTemplate(隊列模式和主題模式)及xml和JavaConfig配置詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Java旋轉(zhuǎn)數(shù)組中最小數(shù)字具體實現(xiàn)(圖文詳解版)
這篇文章主要給大家介紹了關(guān)于Java旋轉(zhuǎn)數(shù)組中最小數(shù)字具體實現(xiàn)的相關(guān)資料,旋轉(zhuǎn)數(shù)組,說明數(shù)據(jù)不變,只是改變位置,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-08-08
Java替換中使用正則表達式實現(xiàn)中間模糊匹配的方法
今天小編就為大家分享一篇Java替換中使用正則表達式實現(xiàn)中間模糊匹配的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

