springboot入门系列教程|第三篇:thymeleaf使用详解之标准表达式

版权声明:转载请注明地址,谢谢配合 https://blog.csdn.net/pulong0748/article/details/82355890

前言

     上一篇我们讲解了springboot如何结合thymeleaf,并且解释了static和templates文件夹的区别,以及介绍了thymeleaf如何解决html5的强校验问题,那么这篇我们就要上一篇我们讲解了springboot如何结合thymeleaf,并且解释了static和templates文件夹的区别,以及介绍了thymeleaf如何解决html5的强校验问题,那么接下来几篇我们就要来讲下thymeleaf的详细用法。

环境

jdk1.8
maven 3.3.9
IDEA 

thymeleaf标准表达式 ##

  • 标准变量表达式

     语法:

语法:${}

     后端代码:

  @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model){
        Person person = new Person() ;
        person.setAge(22);
        person.setName("tom");
        person.setWxCode("coldStone");
        model.addAttribute("person", person);
        return "index";
    }

     前端代码:

<p>
年龄:<span th:text="${person.age}"></span></br>
姓名:<span th:text="${person.name}"></span></br>
微信公众号:<span th:text="${person.wxCode}"></span>
</p>

     访问显示:

这里写图片描述


  • 选择变量表达式
    语法:
*{}

     后端代码:

 @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model){
        Person person = new Person() ;
        person.setAge(22);
        person.setName("xxx");
        person.setWxCode("coldStone");
        model.addAttribute("person", person);
        return "index";
    }

     前端代码:

<p th:object="${person}">
    年龄:<span th:text="*{age}"></span></br>
    姓名:<span th:text="*{name}"></span></br>
    微信公众号:<span th:text="*{wxCode}"></span>
</p>

     说明:

选择表达式使用th:object来绑定后台传来的User对象(通过标准变量表达式),
然后使用 * 来代表这个对象,后面 {} 中的值是此对象中的属性;

     或者直接使用*表达式,两种方式都可以:

<p >
    年龄:<span th:text="*{person.age}"></span></br>
    姓名:<span th:text="*{person.name}"></span></br>
    微信公众号:<span th:text="*{person.wxCode}"></span>
</p>

这里写图片描述

  • URL表达式
    语法:
@{}

     后端代码:

  @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model){
        Person person = new Person() ;
        person.setAge(22);
        person.setName("xxx");
        person.setWxCode("coldStone");
        model.addAttribute("person", person);
        return "index";
    }

     前端代码:

<a href="personInfo.html" 
th:href="@{'http://localhost:8080/coderV/person/info?wxCode='+${person.wxCode}}">
我的微信公众号是:coldStone</a>

这里写图片描述
引出一个问题:假如我要显示的字符串很多,难道我还要像上面前端代码一个个的拼接吗?并不是,我们有语法可以来解决字符串的拼接问题:

<a href="personInfo.html" 
th:href="@{'http://localhost:8080/coderV/person/info?wxCode='+${person.wxCode}+'&name='+${person.name}}">
我的微信公众号是:coldStone</a>

     这段代码看上去是不是特别长,我也就拼了两个而已,我们来看下使用thmeleaf简化后的,注意|…….|里面只可以包含变量表达式,不能包含文本字面量’……………’,所以下面|………..|里面的地址不需要引号

<a href="personInfo.html" th:href="@{|http://localhost:8080/coderV/person/info?wxCode=${person.wxCode}&name=${person.name}|}">我的微信公众号是:coldStone</a>

     很明显这两种结果打印一模一样,但是使用||却更加的简单:
这里写图片描述
     接下来我们来讲解下相对URL:
     第一种相对于页面的URL:

<a href="index.html" th:href="@{'person?wxCode='+${person.wxCode}}">我的微信公众号是:coldStone</a></body></br>

     第二种相对于上下文的URL,就多加了一个/:

<a href="index.html" th:href="@{'/person?wxCode='+${person.wxCode}}">我的微信公众号是:coldStone</a>

     结果,发现加/的多了项目上下文/coderV:

<a href="person?wxCode=coldStone">我的微信公众号是:coldStone</a></body></br>
<a href="/coderV/person?wxCode=coldStone">我的微信公众号是:coldStone</a>

     上一篇:springboot 结合thymeleaf 模板引擎初探
     获取本章源码:github地址
     希望大家支持一下我的微信公众号:coldStone,主要会分享分布式系统相关的一系列技术,目前会推出springboot、springcloud和docker系列教程,后期会有关于中间件以及各个层面的性能优化的文章,同时还会分享一些赚钱理财的小套路哦,欢迎大家来支持,一起学习成长,程序员不仅仅是搬瓦工!
这里写图片描述

猜你喜欢

转载自blog.csdn.net/pulong0748/article/details/82355890
今日推荐