JavaEE:Thymeleaf使用

说明:

Thymeleaf是页面模板,和FreeMarker 类似,替代JSP。

一、SpringBoot整合Thymeleaf:

1.导入依赖包,在工程pom.xml中:

<!-- 继承起步依赖spring-boot-starter-parent -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
</parent>

<dependencies>
    <!-- 导入thymeleaf依赖包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.暂时关闭thymeleaf缓存(实时显示效果),在工程resources/application.yml:

spring:
  thymeleaf:
    cache: false  #设置不缓存,能看实时效果,正式环境改成true

3.其他配合类:

(1)实体类,用于传参的对象:

public class User implements Serializable {
    public String name;
    public int age;
    public long birthday;
    public String imgUrl;
    //...以下为构造方法和set/get方法
}

(2)HTTP请求处理类,用于给页面传参:

@Controller
public class UserController {//处理HTTP请求
    @RequestMapping("/getUserInfo")
    public String getUserInfo(Model model) {
        //给页面传对象
        User user = new User("一个超长的名称", 18, new Date().getTime(), "http://");
        model.addAttribute("user", user);
        //给页面传列表
        User user1 = new User("先生1", 19, new Date().getTime(), "http://");
        User user2 = new User("先生2", 20, new Date().getTime(), "http://");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        model.addAttribute("users", users);
        //给页面传Map
        Map<String, String> map = new HashMap<>();
        map.put("key1", "值1");
        model.addAttribute("map", map);
        //给页面传html标签
        model.addAttribute("htmlstr", "<p>HTML内容</p>");
        return "userinfo"; //跳转到userinfo.html页
    }
}

(3)编写Application类,用于启动服务:

@SpringBootApplication
public class Thymeleafpplication {
    public static void main(String[] args) {
        SpringApplication.run(Thymeleafpplication.class, args);
    }
}

二、Thymeleaf使用:

1.在resources/templates下创建userinfo.html,用于编写Thymeleaf标签:

<!-- 一、导入thymeleaf标签库 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<!-- ...其他HTML自带标签 -->
<body>

<!-- 二、使用thymeleaf标签 -->

1.th:text使用,用于显示文本,${name}为传过来的参数:
格式:th:text="值"
例:
(1)获取并显示对象类型的数据:
<p th:text="${user.name}"></p>
(2)获取并显示map类型的数据:
<p th:text="${#maps.containsKey(map, 'key1')?map.key1:''}"></p><!-- 条件表达式,判断map中是否包含key1 -->
(3)格式化Date类型的数据:
<p th:text="${#dates.format(user.birthday, 'MM-dd')}"></p><!-- 格式化为 月-日 -->
(4)截取字符串,第1个参数为字符串,第2个参数为截取长度,必须大于3,设3显示...,设3以下报错
<p th:text="${#strings.abbreviate(user.name, 4)}"></p>

2.th:utext使用,会解析包含的html标签:
格式:th:utext="值"
例:
<p th:utext="${htmlstr}"></p>

3.th:action使用,用于请求接口:
格式:th:action="@{/请求路径名}"
例:
<form th:action="@{/getUserInfo}">
    <input type="text" th:value="${user.name}">  <!-- 页面回显 -->
    <button>请求getUserInfo接口</button>
</form>

4.th:each使用,循环获取列表中每个item的值:
格式:
th:each="item名称,状态名:${传过来的列表名}
${item名称} //item是对象时:${item名称.属性名}
(1)循环List数据:
<table>
    <tr th:each="item,status:${users}">
        <td th:text="${status.index}"></td>   <!-- 当前position -->
        <td th:text="${item.name}"></td>   <!-- 取item的值 -->
    </tr>
</table>

(2)遍历Map数据:
说明:#maps.containsKey为系统API。
<div th:each="item:${map}">
    <span th:text="${item.key}"></span><!-- 取每个Key -->
    <span th:text="${item.value}"></span><!-- 取每个值 -->
</div>

(3)用numbers.sequence创建数组:
格式:#numbers.sequence(第1个数, 最后1个数)
<div th:each="i:${#numbers.sequence(5, 10)}">
    <span th:text="${i}"></span> <!-- 值为:5 6 7 8 9 10 -->
</div>

5.th:if和th:unless,条件判断:
(2)th:if,条件成立才执行:
格式:th:if="${条件表达式}"
<p th:if="${user.age>=17}">满足条件才显示的内容</p>

(2)th:unless,条件不成立才执行:
格式:th:unless="${条件表达式}"
<p th:unless="${user.age<17}">不满足条件才显示的内容</p>

6.th:fragment和th:include:
(1)th:fragment使用,在a_fra.html中定义模块:
格式:th:fragment="自定义模块名称"
<!--<span th:fragment="fra">待被导入的模块</span>-->

(2)th:include使用,userinfo.html导入a_fra.html页的模块:
格式:th:include="被导入的html文件名:自定义模块名"
<span th:include="a_fra::fra"></span>  <!-- a_frag为html文件名, fra为模块名 -->

7.th:href使用,配置请求地址,可带参数:
格式:h:href="@{${url地址}(参数名='参数值')
<a th:href="@{${user.imgUrl}(key1='value1')}">点击跳到头像显示</a>

</body>
</html>

2.上面第6条(th:fragment和th:include标签)用到的a_fra.html(在resources/templates下创建):

<!DOCTYPE html>
<!-- 导入thymeleaf标签库 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
...
<body>
这是fragment页面
<span th:fragment="fra">这是fragment页面, 待被导入的模块</span>
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值