Thymeleaf:运算符(逻辑语句、遍历、内置对象)


1、逻辑语句

(1)if与unless

 <hr/>
    <p th:if="1==1">if</p>
    <p th:unless="1 ne 1">unless</p>
    <hr/>
    <p th:if="1==2">if</p>
    <p th:unless="1 eq 1">unless</p>

测试结果:

(2)switch ... case语句

书写controller向页面提供数据:

    @RequestMapping("/test")
    public String hello(Model model){
        model.addAttribute("msg",1);
        return "test";
    }

书写html页面:

   <ul th:switch="${msg}">
         <li th:case="1">星期一</li>
         <li th:case="2">星期二</li>
         <li th:case="3">星期三</li>
         <li th:case="4">星期四</li>
         <li th:case="5">星期五</li>
         <li th:case="6">星期六</li>
         <li th:case="7">星期天</li>
         <li th:case="*">输入错误</li>
     </ul>

测试结果:

 提供一个不在范围内的数据:

   @RequestMapping("/test")
    public String hello(Model model){
        model.addAttribute("msg",11);
        return "test";
    }

 

2、遍历

数据:

@Controller
public class TestController { @RequestMapping("/test") public String hello(Model model){ model.addAttribute("users", Arrays.asList("tom","jack","zhai")); return "test"; } }

模板:

<body>
   <h3 th:each="user:${users}" th:text="${user}"></h3>
</body>

测试:

 

3、内置对象

(1)时间

获取当前时间:

<h4>[[${#dates.createNow()}]]</h4>

测试结果:

 日期的格式化:

在controller层获取时间数据并传递到页面:

  @RequestMapping("/test")
    public String hello(Model model){
        model.addAttribute("date",new Date());
        return "test";
    }

在html页面负责获取数据并将日期进行格式化:

<h4>[[${#dates.format(date,'yyyy年MM月dd日')}]]</h4>

测试:

(2)数字处理

小数的的格式化:

<h4>[[${#numbers.formatDecimal(3.1415926,2,3)}]]</h4>

测试结果:

数字的格式化:

<h4>[[${#numbers.formatDecimal(99999999.75433,1,'COMMA',2,'POINT')}]]</h4>

测试结果:

 COMMA是逗号,POINT是点

 

4、字符串处理

(1)截取字符串

<h4>[[${#strings.substring('美国或有60万人死于新冠美国新冠疫情最新消息死亡多少人',0,10)}]]</h4>

测试结果:

(2)字符串处理

<h4>[[${#strings.length(message) le 20 ? message:#strings.substring(message,0,20)+'...'}]]</h4>

测试结果:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM