九、局部变量(local variable)

之前在th:each中遇到过局部变量

<tr th:each="prod : ${prods}">
...
</tr>

其中prod就是局部变量。

除此之外,thymeleaf提供了另外一种声明方式,通过使用th:each,语法如下:

<div th:with="firstPer=${persons[0]}">
<p>The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.</p>
</div>

多个可用逗号隔开:

<div th:with="firstPer=${persons[0]},secondPer=${persons[1]}">
<p>The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.</p>
<p>
But the name of the second person is
<span th:text="${secondPer.name}">Marcus Antonius</span>.
</p>
</div>

th:with属性允许用定义在同一属性内的变量

<div th:with="company=${user.company + ' Co.'},account=${accounts[company]}">...</div>
<p>
Today is:
<span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 february 2011</span>
</p>

Well, 我们想要 "MMMM dd'','' yyyy" 的日期格式,那么我们可以加下面的句子到 home_en.properties 

date.format=MMMM dd'','' yyyy
<p>
Today is:
<span th:with="df=#{date.format}"
th:text="${#calendars.format(today,df)}">13 February 2011</span>
</p>

th:with优先级高于 th:text,所以我们可以放在一个标签内

十、属性优先级(Atrribute Precedence)

 十一、注释 (Comments and Blocks)

11.1  <!-- ... -->  同HTML/XML的注释

<!-- User info follows -->
<div th:text="${...}">
...
</div>

11.2 thymeleaf解析器注释

thymeleaf解析的时候会被注释,静态打开时会显示

单行   <!--/*  ...   */-->

<!--/* This code will be removed at thymeleaf parsing time! */-->

多行时:

<!--/*-->

...

...

<!--/*-->
<!--/*-->
<div>
you can see me only before thymeleaf processes me!
</div>
<!--*/-->

11.3 静态解析时被注释,thymeleaf解析时会移除<!--/*/ 和 /*/-->标签对,内容保留

<span>hello!</span>
<!--/*/
<div th:text="${...}">
...
</div>
/*/-->
<span>goodbye!</span>

这个注释在写th:block的时候很有用哦

<table>
<!--/*/ <th:block th:each="user : ${users}"> /*/-->
<tr>
<td th:text="${user.login}">...</td>
<td th:text="${user.name}">...</td>
</tr>
<tr>
<td colspan="2" th:text="${user.address}">...</td>
</tr>
<!--/*/ </th:block> /*/-->
</table>

11.4 th:block 适用于比如说要循环两个<tr>

it could be useful, for example, when creating iterated tables that require more than one <tr> for each element

thymeleaf 局部变量、属性优先级、注释的更多相关文章

  1. thymeleaf的属性优先级

    所有Thymeleaf属性定义一个数字优先,建立他们的顺序执行的标签.这个顺序是: Order Feature Attributes 1 Fragment inclusion th:includeth ...

  2. (八)Thymeleaf的 th:* 属性之—— 模板布局& th:with& 属性优先级

    3.7 模板布局 模板名称:layout.html 3.7.1 th:fragment e.g.模板名为footer.html页面body部分如下: <body> <div th:f ...

  3. 设置eclipse中自动添加get,set的注释为字段属性的注释

    一:说明 首先具体来看一下是什么效果,上图可能会更清楚一点 就是在get/set中自动加上属性的注释,那我们要怎么配置呢? 二:配置 2.1:下载附件 下载附件 2.2:替换class 原生的ecli ...

  4. 如何在Sql2008中获取表字段属性和注释?

    如何在Sql2008中获取表字段属性和注释? select b.[value] from sys.columns a left join sys.extended_properties b on a. ...

  5. spring boot 与 thymeleaf (3): 设置属性、条件、遍历、局部变量、优先级、内联语法

    前面记录了 thymeleaf 基本表达式, 这里继续看一下其他功能. 一. 设置属性值 这里的controller, html框架 还是沿用上一篇的部分. html: <div class=& ...

  6. Thymeleaf 常用属性

    Thymeleaf 常用属性 如需了解thymeleafThymeleaf 基本表达式,请参考<Thymeleaf 基本表达式>一文 th:action 定义后台控制器路径,类似<f ...

  7. javascript 构造函数中的属性与原型上属性优先级的比较

    备注: 下面这个问题是我前天看书上发现的. 按照我以前的理解, a.rename()这个方法传什么值,结果都会弹出 小a,但我看书上的demo 弹出的是大A.... 我的困惑是:  js的构造函数中的 ...

  8. C#封装程序集属性方法注释说明

    一.使用封装程序集好处: 在很多分布式应用程序开发中,针对每一种功能可能条用的接口不一样,往往习惯将需要被调用的接口,封装成DLL给调用方应用后使用,这样既规范了调用的方式,又避免了调用出现参数请求方 ...

  9. js变量定义提升、this指针指向、运算符优先级、原型、继承、全局变量污染、对象属性及原型属性优先级

    原文出自:http://www.cnblogs.com/xxcanghai/p/5189353.html作者:小小沧海 题目如下: function Foo() { getName = functio ...

随机推荐

  1. LeetCode45 Jump Game II

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  2. 琐碎-hadoop2.2.0-hbase0.96.0-hive0.13.1整合

    关于hadoop和hive.hbase的整合就不说了,这里就是在hadoop2.2.0的环境下整合hbase和hive 因为hive0.12不支持hadoop2,所以还要替换一些hadoop的jar包 ...

  3. Windows 之 删除保存的共享凭据(用户名和密码)

    当我们在访问Windows共享文件夹或者NAS网络共享盘的时候,Windows会提示输入访问共享所需要的用户名和密码,如果我们勾选了“记住我的凭据”,Windows 就会将认证凭据保存到计算机中,以方 ...

  4. CSS浮动属性Float到底什么怎么回事,下面详细解释一下

    float 是 css 的定位属性.在传统的印刷布局中,文本可以按照需要围绕图片.一般把这种方式称为“文本环绕”.在网页设计中,应用了CSS的float属性的页面元素就像在印刷布局里面的被文字包围的图 ...

  5. (Android学习系列)二,窗口(Activity)的生命周期

    在Activity从创建到销毁的过程中需要在不同的阶段调用7个生命周期的方法这7个生命周期方法定义如下: protected void onCreate(Bundle savedInstanceSta ...

  6. 剑指Offer02 替换空格

    /************************************************************************* > File Name: 02_Replac ...

  7. Flume与Kafka集成

    一.Flume介绍 Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的能 ...

  8. BZOJ 2819: Nim dfs序维护树状数组,倍增

    1.随机选两个堆v,u,询问若在v到u间的路径上的石子堆中玩Nim游戏,是否有必胜策略,如果有,vfleaking将会考虑将这些石子堆作为初始局面之一,用来坑玩家.2.把堆v中的石子数变为k. 分析: ...

  9. 有一种风格,叫做 Low Poly 3D

    原作:Simon阿文    杂交编辑者:RhinoC       个人更推崇使用第二款神器 ImageTriangulator :http://www.conceptfarm.ca/2013/port ...

  10. photoSlider-原生js移动开发轮播图、相册滑动插件

    详细内容请点击 在线预览   立即下载 使用方法: 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css& ...