Spring boot 中使用 Thymeleaf
Thymeleaf 是一个java类库,他是一个xml/xhtml/html5的模板引擎,可使用在MVC的Web应用的View层.
创建springboot项目,并引入Thymeleaf:
下面附上部分代码
SomunsAppcation.java
package net.somuns.somuns;import net.somuns.somuns.model.Dog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.ArrayList;@Controller
@SpringBootApplication
public class SomunsApplication {@RequestMapping("/")public String index(Model model){ArrayList<Dog> dogList = new ArrayList<>();dogList.add(new Dog("小黄","黄色",1));dogList.add(new Dog("小红","黑色",3));dogList.add(new Dog("旺财","棕色",2));dogList.add(new Dog("花花","白色",1));model.addAttribute("dogList",dogList);model.addAttribute("dog",new Dog("泰迪","棕色",2));return "index";}public static void main(String[] args) {SpringApplication.run(SomunsApplication.class, args);}
}
index.html
<html xmlns:th="http://www.thymeleaf.org">
<head><meta content="text/html" charset="UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1" /><link th:href="@{css/bootstrap.css}" rel="stylesheet" /><title>Thymeleaf</title>
</head>
<body>
<div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title">单个对象—model</h3></div><div class="panel-body"><span th:text="${dog.name}"></span></div>
</div>
<div th:if="${not #lists.isEmpty(dogList)}"><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title">列表-集合</h3></div><div class="panel-body"><ul class="list-group"><li class="list-group-item" th:each="dog:${dogList}"><span th:text="${dog.name}"></span><span th:text="${dog.color}"></span><span th:text="${dog.age}"></span><button class="btn" th:οnclick="'getName(\''+${dog.name}+'\');'">获得名字</button></li></ul></div></div>
</div>
<script th:src="@{js/jquery-3.2.1.min.js}" type="text/javascript"></script>
<script th:src="@{js/bootstrap.min.js}"></script>
<script th:inline="javascript">var dog_ = [[${dog}]];console.log(dog_.name+"/"+dog_.color);function getName(name){alert(name);}
</script>
</body>
</html>
程序成功后:
附上码云实例代码:https://gitee.com/xushoulai/somuns-demo/tree/master/springboot-thymeleaf
Spring boot 中使用 Thymeleaf相关推荐
- 8.Spring Boot中使用thymeleaf
Spring Boot中使用thymeleaf Spring Boot支持FreeMarker.Groovy.Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf. s ...
- 在Spring Boot中配置Thymeleaf的模板路径
众所周知,Thymeleaf的模板文件默认是在项目文件夹的src\main\resources\templates目录下的.不过出于特殊需要,要修改其路径怎么办呢? 在我们的项目配置文件applica ...
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
- Spring Boot中Thymeleaf的初步使用
目录 理论 演示 理论 使用TemplateEngine是Spring Boot中推荐的,他的作用是: 把模板(如html界面)和数据匹配好,然后输出,发给用户. 而不是传统的使用jsp进行操作 模版 ...
- 再谈Spring Boot中的乱码和编码问题
编码算不上一个大问题,即使你什么都不管,也有很大的可能你不会遇到任何问题,因为大部分框架都有默认的编码配置,有很多是UTF-8,那么遇到中文乱码的机会很低,所以很多人也忽视了. Spring系列产品大 ...
- Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台项目
项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于 Layui 的后台管理系统模板,扩展 Layui 原生 U ...
- Spring Boot中Starter是什么
比如我们要在Spring Boot中引入Web MVC的支持时,我们通常会引入这个模块spring-boot-starter-web,而这个模块如果解压包出来会发现里面什么都没有,只定义了一些POM依 ...
- java中的controller_详解Spring Boot中Controller用法
Controller Controller是SpringBoot里最基本的组件,他的作用是把用户提交来的请求通过对URL的匹配,分配个不同的接收器,再进行处理,然后向用户返回结果.他的重点就在于如何从 ...
- twilio_15分钟内使用Twilio和Stormpath在Spring Boot中进行身份管理
twilio 建筑物身份管理,包括身份验证和授权? 尝试Stormpath! 我们的REST API和强大的Java SDK支持可以消除您的安全风险,并且可以在几分钟内实现. 注册 ,再也不会建立au ...
最新文章
- 模型压缩6倍,无需重训练:数学家团队提出量化新方法
- LVM是逻辑盘卷管理1
- @angular/platform-browser-dynamic
- Django加载静态文件
- 关于自动化测试,你真的懂嘛?
- 让apache支持ssl
- fileboy v1.9 发布,文件变更监听通知系统
- 在线类图各种UML图分享
- 走在技术的前沿 创造一代名品
- WPS插入页码,自动生成目录
- 【北京迅为】i.MX6ULL终结者虚拟机安装Ubuntu系统
- 基于Java框架开发OA企业在线办公系统项目教程-附源码-毕业设计
- 学计算机的女生容易脱单,最容易让男生脱单的5个大学专业,特别是第3个,女生会倒追你!...
- 关于NoSQL与SQL的区别
- 2021年教你增加拿到BAT等大厂offer几率,分享PDF高清版
- react antd design columns 配置解析
- 几何画板在教学中的作用
- 用图章工具 修改数字
- 2021.12.9 java代码对接sap接口(soap协议、webservice)
- 2011年5月51CTO壁纸点评活动获奖名单【已结束】