Spring Boot 7-SpringBoot Web Thymeleaf模板引擎
1、springboot的对web的开发支持。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
springboot支持大量模板引擎(Thymeleaf、FreeMaker、Velocity、Groovy、Mustache)。
springboot建议使用这些模板引擎,避免使用jsp(springboot有的功能jsp无法支持)。
其中在SpringBoot中Thymeleaf可以完全替代jsp,作为mvc的web应用的view层。
下面介绍一下Thymeleaf的使用:
2、thymeleaf的引入。
2.1、命名空间xmlns:th="http://www.thymeleaf.org,要进行动态处理的元素以th开头。
2.2、通过@{}引用web静态资源。
2.3、通过${}访问model下的属性。
2.4、js取model值[[${}]],th:inline="javascript"
2.5、遍历集合th:each="infos:${list}"
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<link th:src="@{css/bootstrap.min.css}" rel="stylesheet">
<title>Insert title here</title>
</head>
<body>
<h1 th:text="${mymodel.title}"></h1>
<ul th:each="infos:${list}" class="list-group">
<li th:text="${infos}" class="list-group-item"></li>
</ul>
<script type="text/javascript" th:src="@{js/jquery-3.3.1.slim.min.js}"></script>
<script type="text/javascript" th:src="@{js/bootstrap.min.js}"></script>
</body>
<script th:inline="javascript">
var myinfo = [[${mymodel.title}]];
console.log(myinfo);
</script>
</html>
3、springboot配置文件配置thymeleaf(cache默认true,开发时请关闭)
对应properties类ThymeleafProperties.java
spring: thymeleaf: cache: false prefix: classpath:/templates/ suffix: .html encoding: UTF-8 mode: HTML5
