SpringBoot引入模板引擎實(shí)現(xiàn)視圖解析
視圖解析
SpringBoot 不支持 JSP,需要引入第三方模板引擎進(jìn)行技術(shù)進(jìn)行頁面渲染
1. 視圖解析方式
轉(zhuǎn)發(fā)、重定向以及自定義視圖
2. 使用
在 pom.xml 文件中引入 Starter
<!--導(dǎo)入Thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
3. 原理
thymeleaf 自動配置類 —— ThymeleafAutoConfiguration.class
@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(ThymeleafProperties.class) @ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class }) @AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class }) public class ThymeleafAutoConfiguration {}
自動配好的策略:
(1)所有的配置值都在 ThymeleafProperties
(2)配好了 SpringTemplateEngine
(3)配好了 ThymeleafViewResolverConfiguration
因此我們只需要開發(fā)頁面
頁面開發(fā)規(guī)則 —— ThymeleafProperties.class
public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html";
規(guī)則解釋:
- 頁面放在 /templates/ 里
- 頁面都是 .html
一個小 Demo:
HTML 端
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <!--重點(diǎn)一: 導(dǎo)入 thymeleaf--> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1 th:text="${msg}">haha</h1> <!--通過th + $ 的方式取出值--> <h2> <a href="www.atguigu.com" rel="external nofollow" rel="external nofollow" th:href="${link}" rel="external nofollow" >去百度</a> <!--$符直接取值當(dāng)成訪問路徑--> <a href="www.atguigu.com" rel="external nofollow" rel="external nofollow" th:href="@{/link}" rel="external nofollow" >去百度</a> <!--@符拼接訪問路徑,自動加前置路徑--> </h2> </body> </html>
viewTestController 類
package com.example.demo2.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class viewTestController { @GetMapping("/haha") public String testhaha(Model model){ // model 中的數(shù)據(jù)會被放到請求域中 model.addAttribute("msg", "要好好學(xué)鴨~"); model.addAttribute("link","https://www.baidu.com/"); return "success"; // 轉(zhuǎn)發(fā)到 success.html (不寫后綴) } }
訪問 http://localhost:8080/haha 可以看到 thymeleaf 自動幫我們渲染好的頁面
到此這篇關(guān)于SpringBoot引入模板引擎實(shí)現(xiàn)視圖解析的文章就介紹到這了,更多相關(guān)SpringBoot視圖解析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot如何使用filter對request body參數(shù)進(jìn)行校驗
這篇文章主要介紹了Springboot如何使用filter對request body參數(shù)進(jìn)行校驗,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot如何使用@Aspect注解實(shí)現(xiàn)AOP
這篇文章主要介紹了SpringBoot如何使用@Aspect注解實(shí)現(xiàn)AOP問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Java結(jié)構(gòu)型設(shè)計模式之適配器模式詳解
適配器模式,即將某個類的接口轉(zhuǎn)換成客戶端期望的另一個接口的表示,主要目的是實(shí)現(xiàn)兼容性,讓原本因為接口不匹配,沒辦法一起工作的兩個類,可以協(xié)同工作。本文將通過示例詳細(xì)介紹適配器模式,需要的可以參考一下2022-09-09SpringBoot如何打印mybatis的執(zhí)行sql問題
這篇文章主要介紹了SpringBoot如何打印mybatis的執(zhí)行sql問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03java實(shí)現(xiàn)在普通類中注入service或mapper
這篇文章主要介紹了java實(shí)現(xiàn)在普通類中注入service或mapper的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07java編程FinalReference與Finalizer原理示例詳解
這篇文章主要為大家介紹了java編程FinalReference與Finalizer的核心原理以及示例源碼的分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01