thymeleaf 标准表达式


thymeleaf 标准表达式

 

${..}:读取对象

*{..}:${..}功能增强,还可读取当前对象

 

#{..}:读取属性文件,常用于读取国际化文件

<span th:text="#{hello}"></span>  //读取国际化属性文件中key:hello的value

 

@{..}:url链接、本地文件链接

<script th:src="@{/jquery/jquery-3.5.1.min.js}"></script>  //导入 jquery 文件

<a th:href="@{http://www.baidu.com}">百度一下</a><br>       //超链接

 

~{..}:引入页面模板

<div th:insert="~{commons :: main}">...</div>  //插入commons.html 中 th:fragment为main的局部页面

 

 

*******************

示例

 

***************

controller 层

 

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(ModelAndView mv){
        Person person=new Person();
        person.setId(1);
        person.setName("瓜田李下");
        person.setAge(20);
        mv.addObject("person",person);

        mv.setViewName("test");
        return mv;
    }
}

 

***************

前端页面

 

test.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:align="center" style="color: orange">
    <strong>
        <span th:text="${person.id}"></span> ==>
        <span th:text="${person.name}"></span> ==>
        <span th:text="${person.age}"></span>
    </strong>
</div><br>

<div th:align="center" style="color: aquamarine" th:object="${person}">
    <strong>
        <span th:text="*{id}"></span> ==>
        <span th:text="*{name}"></span> ==>
        <span th:text="*{age}"></span>
    </strong>
</div><br>

<div th:align="center" style="color: purple">
    <strong>
        <span th:text="*{person.id}"></span> ==>
        <span th:text="*{person.name}"></span> ==>
        <span th:text="*{person.age}"></span>
    </strong>
</div>
</body>
</html>

 

 

*******************

使用测试

 

localhost:8080/hello

                         

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值