Spring Boot中使用thymeleaf

2017-09-30

Spring Boot支持FreeMarker、Groovy、Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf。

spring-boot-starter-thymeleaf

在Spring Boot中使用Thymeleaf只需在pom中加入Thymeleaf的starter即可:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在Spring Boot 1.5.9.RELEASE版本中,默认的Thymeleaf版本为2.1.6.RELEASE版本,这里推荐使用3.0以上版本。在pom中将Thymeleaf的版本修改为3.0.2.RELEASE:

Read More »


Thymeleaf 局部变量

2017-09-30

在Thymeleaf模板引擎中,使用 th:with属性来声明一个局部变量:

1
2
3
<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:width属性声明了一个名为firstPer的局部变量,内容为${persons[0]}。该局部变量的作用域为整个div内。

Read More »


Thymeleaf 模板布局

2017-09-18

th:include,th:insert和th:replace

在模板的编写中,通常希望能够引入别的模板片段,比如通用的头部和页脚。Thymeleaf模板引擎的th:include,th:insert和th:replace属性可以轻松的实现该需求。不过从Thymeleaf 3.0版本后, 不再推荐使用th:include属性。

在index.html页面路径下创建一个footer.html:

1
2
3
4
5
6
7
8
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<footer th:fragment="copy">
&copy; 2016 - 2017 MrBird Powered by Hexo
</footer>
</body>
</html>

Read More »

Thymeleaf 条件语句

2017-09-15

if 与 unless

假如现在有一个商品列表,当商品有评论时,显示view按钮,否则不显示。这时候就可以使用Thymeleaf的th:if标签来实现:

1
2
3
<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>

当prod.comments不为空时,页面将渲染出该<a>标签。

另外,th:if有一个反向属性th:unless,用于代替上面的not:

1
2
3
<a href="comments.html"
th:href="@{/comments(prodId=${prod.id})}"
th:unless="${#lists.isEmpty(prod.comments)}">view</a>

Read More »

Thymeleaf 迭代

2017-09-13

在Thymeleaf中,使用th:each标签可对集合类型进行迭代,支持的类型有:

1.任何实现了java.util.List的对象;

2.任何实现了java.util.Iterable的对象;

3.任何实现了java.util.Enumeration的对象;

4.任何实现了java.util.Iterator的对象;

5.任何实现了java.util.Map的对象。当迭代maps时,迭代变量是java.util.Map.Entry类型;

Read More »


Thymeleaf 表达式工具类

2017-09-10

Thymeleaf默认提供了丰富的表达式工具类,这里列举一些常用的工具类。

Objects工具类

1
2
3
4
5
6
7
8
/*
* 当obj不为空时,返回obj,否则返回default默认值
* 其同样适用于数组、列表或集合
*/
${#objects.nullSafe(obj,default)}
${#objects.arrayNullSafe(objArray,default)}
${#objects.listNullSafe(objList,default)}
${#objects.setNullSafe(objSet,default)}
Read More »

Thymeleaf 标准表达式语法

2017-09-06

记录几个比较容易忘记的Thymeleaf标准表达式语法,例子基于Spring MVC。

变量表达式${ }

在控制器中往页面传递几个变量:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Controller
public class IndexController {

@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(HttpSession session, Model model){
User user = new User();
user.setName("KangKang");
user.setAge(25);
user.setHabbit(new String[]{"football","basketball","swim"});
session.setAttribute("user", user);
model.addAttribute(user);
return "index";
}
}

Read More »

自定义annotation

2017-09-05

meta-annotation

Java从JDK5.0开始便提供了四个meta-annotation用于自定义注解的时候使用,这四个注解为:@Target,@Retention,@Documented 和@Inherited。

@Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方),其源码如下:

1
2
3
4
5
6
7
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {

ElementType[] value();
}

Read More »

1…242526…45
Hosted  by  Coding Pages
MrBird
MrBird

A simple blog, code repository, just keep blogging

14 Archives 2 Labels
  • 🏠 Home
  • 📦 Archives
  • 🔖 Labels
  • 👬 Friends
  • 🔍 Search
  •   UV    PV 
    0