Thymeleaf 入门
生活随笔
收集整理的這篇文章主要介紹了
Thymeleaf 入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基本項目結構:
?
Thymeleaf配置:
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/static/
spring.thymeleaf.suffix=.html
spring默認的模板映射路徑是:src/main/resources/templates
?ftl路徑的hello.html:
<!DOCTYPE html>
<html lang="zh-CN"xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"><head><title>helloWorld</title></head><body><h1>Hello : <b th:text="${name}">姓名</b></h1></body>
</html>
TestController:
?
package com.eshore.ismp.web;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@Controller
public class TestController {@RequestMapping(value = "test", method = RequestMethod.GET)public String test(Model model) {model.addAttribute("name", "MERCY");return "/ftl/hello";}}
運行結果:
?
?要注意的點:
spring.thymeleaf.cache要設置為true,以方便開發及時看到頁面刷新
springboot直接引入:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
非springboot項目引入:
<dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>2.1.4</version>
</dependency>
LEGACYHTML5屬性值需要搭配一個額外的庫NekoHTML才可用。主要作用是設置Thymeleaf格式檢查沒這么嚴格,默認屬性值是HTML5
?
轉載于:https://www.cnblogs.com/JAYIT/p/10444763.html
總結
以上是生活随笔為你收集整理的Thymeleaf 入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java设计模式----装饰器模式
- 下一篇: 链表学习【随笔】