标签:class font 基础 control marker height sep bsp star
模板引擎
Spring Boot可以完美的实现动态HTML,Spring Boot提供了多种模板引擎。
Spring Boot推荐使用Thymeleaf模板引擎,原因在于它提供了完美的Spring MVC支持。
一、Thymeleaf基础知识
Thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非Web环境中的应用开发,是一个java类库。可以使用它完全替代View层。
二、示例
导入Thymeleaf依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
编写Controller
@Controller public class HelloController { @GetMapping("hello") public String getHello(ModelMap modelMap){ modelMap.addAttribute("name","Thymeleaf"); return "index"; } }
在默认的模板路径src/main/resources/templates
下编写模板文件
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>Thymeleaf示例</title> </head> <body> <h1 th:text="${name}">Hello World</h1> </body> </html>
(
xmlns:th="http://www.thymeleaf.org":命名空间,需要进行动态处理的元素将使用"th:"为前缀。
)
启动程序后,访问http://localhost:8080/
,则是展示Controller中name的值:Thymeleaf
三、Thymeleaf的默认参数配置
# Enable template caching.
spring.thymeleaf.cache=true
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true
# Content-Type value.
spring.thymeleaf.content-type=text/html
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true
# Template encoding.
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names=
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
标签:class font 基础 control marker height sep bsp star
原文地址:http://www.cnblogs.com/tianzhebuzhu/p/7625266.html