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>


springboot支持大量模板引擎,其中在SpringBoot中Thymeleaf可以完全替代jsp,作为mvc的web应用的view层。

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





猜你喜欢

转载自www.cnblogs.com/zengpingtang/p/10809138.html