springboot之themeleaf模板引擎八

xiaoxiao2021-02-28  85

springboot提供了大量的模板引擎,包含FreeMarker、Groovy、Thymeleaf、Velocity和Mustache,springboot中推荐使用

thymeleaf作为模板引擎,因为thymeleaf提供了完美的spring MVC的支持。

Thymeleafsh是一个java类库,是xml/xhtml/htmMVCl5的模板引擎,可以作为MVC的web应用的view层。

下面举个例子看看thymeleaf是如何替代jsp的:

1、pom.xml文件引入Thymeleaf的包

                <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

2、访问model中的数据

通过${}访问model中的属性

<html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> 123456789 <ul> <li th:each="person:${people}"> <span th:text="${person.name}"></span> <span th:text="${person.age}"></span> </li> </ul> </body> </html>

使用th:each来做循环遍历迭代,其中person是迭代元素

3、js访问model中的数据

通过th:inline="javascript"添加到script标签,这样javascript代码就可以访问到model中的属性

通过“[[${}]]”格式获取实际的值

4、model层的数据处理

5、执行程序

转载请注明原文地址: https://www.6miu.com/read-1950090.html

最新回复(0)