springboot之themeleaf模板引擎八

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、执行程序



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值