模版引擎雖然不能夠?qū)崿F(xiàn)代碼與視圖解耦,但是其適合于個(gè)人開(kāi)發(fā)者使用,而且如果存在前后端項(xiàng)目中,前端大量請(qǐng)求后端時(shí),模版引擎無(wú)疑也存在優(yōu)勢(shì)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-536082.html
SpringBoot 整合步驟:
- 引入依賴
- 編寫 yml 配置
- 編寫 html 模版文件
- 編寫 Controller 接口
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
spring:
thymeleaf:
enabled: true #開(kāi)啟thymeleaf視圖解析
encoding: utf-8 #編碼
prefix: classpath:/templates/ #前綴
cache: false #是否使用緩存
mode: HTML #嚴(yán)格的HTML語(yǔ)法模式
suffix: .html #后綴名
<!DOCTYPE html>
<!--標(biāo)記 thymeleaf 語(yǔ)法-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> Hello <span th:text="${username}"></span>
</h1>
</body>
</html>
// 此處使用 @Controller注解 與 ModelAndView 進(jìn)行視圖選擇、傳參
@Controller
public class User {
@GetMapping("user")
ModelAndView get() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user"); // 視圖選擇
modelAndView.addObject("username", "小明"); // 傳參
return modelAndView;
}
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-536082.html
到了這里,關(guān)于Thymeleaf模版引擎初嘗試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!