檢視解析與模板引擎thymeleaf
阿新 • • 發佈:2021-03-27
檢視解析:SpringBoot預設不支援JSP,需要引入第三方模板引擎技術實現頁面渲染。
檢視解析
模板引擎thymeleaf
thymeleaf使用
- 引入starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 自動配置好thymeleaf
@Configuration (proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration { }
自動配置好的策略
1、所有thymeleaf的配置值都在 ThymeleafProperties
2、配置好了 SpringTemplateEngine
3、配好了 ThymeleafViewResolver
4、我們只需要直接開發頁面
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html"; //xxx.html
前端頁面就放在templates裡面
頁面開發
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${msg}">ahh</h1>
<h2>
<a href="前端隨便寫一個預設的地址" th:href="${link}">去百度</a>
<a href="前端隨便寫一個預設的地址" th:href="@{link}">去百度2</a>
</h2>
</body>
</html>
在瀏覽器檢視網頁原始碼我們發現,使用${link}取到了網頁url地址,而@{link}則不行。