JAVA入门[22]—thymeleaf
一、thymeleaf官网
官网:https://www.thymeleaf.org/index.html
doc:https://www.thymeleaf.org/documentation.html
二、springmvc+thymeleaf从这里开始
1.修改pom.xml,引入相关依赖。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependendies>
2.xml方式配置thymeleaf视图解析器:
<!-- Thymeleaf View Resolver - implementation of Spring's ViewResolver interface -->
<bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
</bean> <!-- Thymeleaf Template Engine (Spring4-specific version) -->
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolvers">
<set>
<ref bean="templateResolver" />
</set>
</property>
</bean> <!-- Thymeleaf Template Resolver -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="templateMode" value="HTML" />
<property name="suffix" value=".html"></property>
<property name="characterEncoding" value="UTF-8"></property>
</bean>
3.在controller中为变量name赋值。
@RequestMapping(value="/index")
public String index(Model model){
model.addAttribute("name","world");
return "index.html";
}
4.在index.html中使用thymeleaf语法读取变量name的值。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Title</title>
</head>
<body>
<div>your name is:<span th:text="${name}"></span></div>
</body>
</html>
注意:需要修改html节点,添加xmlns:th="http://www.thymeleaf.org"
三、thymeleaf常见问题小结
1.如何添加链接:
<a th:href="@{/category/index}">首页</a>
<a class="btn btn-xs btn-default" th:href="@{/role/edit(roleId=${r.roleId})}">编辑</a>
2.表单绑定示例:
<form method="post" th:object="${cate}" th:action="@{/category/save}" enctype="multipart/form-data">
<table>
<tr>
<td>id:</td>
<td><input type="text" th:field="*{cateId}"></td>
</tr>
<tr>
<td>name:</td>
<td><input type="text" th:field="*{cateName}"></td>
</tr>
<tr>
<td>file:</td>
<td>
<input type="file" accept="image/jpeg,image/png,image/jpg" name="picture">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
3.展示文本
<td th:text="${r.name}"></td>
如何替换子字符串
<span th:text="|welcome,${name}|"></span>
如何转换日期格式
${#dates.format(v.AddDate,'yyyy-MM-dd HH:mm:ss')}
4.如何在js读取后台数据
var url="[[${url}]]";
5.条件表达式
<td th:text="(${r.deleteFlag}==0)?'正常':'删除'"></td>
6.thymeleaf如何实现switch选择
<td th:switch="${r.type}">
<span th:case="page">页面</span>
<span th:case="module">模块</span>
<span th:case="*">其他</span>
</td>
注意:默认值 用th:case="*"
7.th:object语法
首先在controller为对象赋值
@Controller
@RequestMapping("/demo")
public class DemoController {
@RequestMapping("/index")
public String index(Model model){
OrgResource resource=new OrgResource();
resource.setId("11");
resource.setName("test");
model.addAttribute("resource",resource);
return "demo/index.html";
}
}
使用*{}语法可以方便读取th:object对象的属性。
<div th:object="${resource}">
<div th:text="*{id}"></div>
<div th:text="*{name}"></div>
</div>
8.迭代 th:each
<th:block th:each="r,iterstat:${resources}">
<tr th:class="${iterstat.odd}?'odd'">
<td th:text="${r.orderNo}"></td>
<td th:switch="${r.type}">
<span th:case="page">页面</span>
<span th:case="module">模块</span>
</td>
<td th:text="(${r.deleteFlag}==0)?'正常':'删除'"></td>
<td th:switch="${r.deleteFlag}">
<span th:case="0"><a>删除</a></span>
<span th:case="1"><a>恢复</a></span>
</td>
</tr>
</th:block>
9.如何使用Fragment layout布局
首先新建layout.html作为布局模板。
<!DOCTYPE html>
<html lang="zh-CN" xmlns:layout="http://www.thymeleaf.org" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
……
<body>
<div layout:fragment="content"></div>
</body>
</html>
然后在index.html中使用layout,并用页面具体内容替代content fragment。
<!DOCTYPE html>
<html layout:decorator="task/layout" xmlns:layout="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div layout:fragment="content">
测试页面
</div>
</body>
</html>
第一次使用layout布局的时候,调试了好半天就是不生效。后来找到了原因,dependency需要添加:
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>${nz.net.ultraq.thymeleaflayout-version}</version>
</dependency>
10.如何用if条件动态调整form action
<form th:action="@{/Video/{path}(path=${isCollect}?'CollectVideoList':'VideoList')}">
<script type="text/plain" id="content" name="content" th:utext="${article.content}"></script>
JAVA入门[22]—thymeleaf的更多相关文章
- JAVA入门--目录
在此记录自己的JAVA入门笔记,备忘 JAVA入门[1]--安装JDK JAVA入门[2]-安装Maven JAVA入门[3]—Spring依赖注入 JAVA入门[4]-IntelliJ IDEA配置 ...
- 新一代Java模板引擎Thymeleaf
新一代Java模板引擎Thymeleaf-spring,thymeleaf,springboot,java 相关文章-天码营 https://www.tianmaying.com/tutorial/u ...
- Java入门基础(变量、操作符与表达式)
Java入门基础 1. 第一个程序 2.变量(命名.运算.整数/小数/字符串.布尔类型) 3.操作符与表达式(算术/逻辑/关系/赋值/自增/类型转换操作符) HelloWorld! public cl ...
- Java入门(3)
阅读书目:Java入门经典(第7版) 作者:罗格斯·卡登海德 在程序中使用字符值时,必须用单引号将赋给变量的字符值括起来,对于字符串必须用双引号括起来. int整型-2.14*10^9~2.14*10 ...
- Java安全之Thymeleaf SSTI分析
Java安全之Thymeleaf SSTI分析 写在前面 文章首发:https://www.anquanke.com/post/id/254519 最近看了一遍Thymeleaf,借此机会学习一下Th ...
- Java入门第一章
后天就是十一长假了,亲们准备好了去哪儿玩了吗? 今天有点空,就来聊聊Java吧,当然是一些Java入门知识了,网上有很多,这里我只是列举一些我自己学到的,感谢大家关注喵的博客这么久,也为大家带来点新知 ...
- [Java入门笔记] 面向对象编程基础(二):方法详解
什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...
- 第1章Java入门体验
第1章Java入门体验 1.java简介和平台应用 Java是sun公司开发出来,现在属于ORACLE公司java分为几个部分:首先是最基础的Java SE部分,这部分是Java的基础知识,主要包括: ...
- Java入门记(五):容器关系的梳理(下)——Map
注意:阅读本文及相关源码时,需要数据结构相关知识,包括:哈希表.链表.红黑树. Map是将键(key)映射到值(value)的对象.不同的映射不能包含相同的键:每个键最多只能映射到一个值.下图是常见M ...
随机推荐
- 记一次调试串口设备Bug的经历
最近花了差不多1天的时间在折腾一个Bug,该Bug的表象如下: 这个Bug还特别独特,在开发电脑中无提示,在终端用户那里每次使用软件的时候都报这个.仔细思考了一下最近在源码中新添加的功能,没发现有啥特 ...
- ionic2 跳转子页面隐藏底部导航栏
第一种方法: 在tab里面添加一个属性[tabsHideOnSubPages]='true' <ion-tab [root]="tab1Root" [tabsHideOnSu ...
- 条件随机场CRF(一)从随机场到线性链条件随机场
条件随机场CRF(一)从随机场到线性链条件随机场 条件随机场CRF(二) 前向后向算法评估观察序列概率(TODO) 条件随机场CRF(三) 模型学习与维特比算法解码(TODO) 条件随机场(Condi ...
- nodejs构建多房间简易聊天室
1.前端界面代码 前端不是重点,够用就行,下面是前端界面,具体代码可到github下载. 2.服务器端搭建 本服务器需要提供两个功能:http服务和websocket服务,由于node的事件驱动机制, ...
- Winform中Chart图表的简单使用
在常见的一些数据采集的系统中, 都少不了一个就是, 数据分析, 无论是报表的形式, 还是图形的形式. 他都是可以迅速的展现一个数据趋势的实现方法, 而今天, 就是简单介绍一下, 微软的工具库自带的 C ...
- react-native 学习 ----- React Navigation
很久没有的登陆博客园了,密码都是找回的,从当年的大学生已经正常的走上了程序员的道路,看到之前发的博客还是写的android,现在自己已经在使用了react-native了. 大学毕业了,做了java后 ...
- 通知栏Notification的整理
一.介绍 通知栏适用于交互事件的通知,是位于顶层可以展开的通知列表. 二.功能作用 1.显示接收到短消息,及时消息等信息(如QQ.微信.新浪.短信) 2.显示客户端的推送消息(如有新版本发 ...
- 【Android Developers Training】 91. 解决云储存冲突
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 屏幕适配/autoLayout autoresizingMask
#pragma mark-- 屏幕适配/autoLayout autoresizingMask 1> 发展历程 代码计算frame -> autoreszing(父控件和子控件的关系) - ...
- Python文件系统功能:os模块
Python文件系统功能:os模块 1.os模块方法分类 (1)目录: chdir() 改变工作目录 chroot() 设定当前进程的根目录 listdir() 列出指定目录下的所有文件名 mkdir ...