Thymeleaf条件判断

1.创建的对象

package com.cc.springboot.entity;

public class User {
    private String username;
//    1:女 2:男
    private Integer gender;

    public User() {
    }

    public User(String username, Integer gender) {
        this.username = username;
        this.gender = gender;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }
}

 2.controller

package com.cc.springboot.controller;

import com.cc.springboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/success")
    public String hello(){
        return "hello cc";
    }


    @RequestMapping("/study")
    public String study(Map<String, Object> map){
        List<User> userList=new ArrayList<>();
        userList.add(new User("小梦",1));
        userList.add(new User("小里",2));
        userList.add(new User("小岚",1));
        map.put("userList",userList);
//        1 :女  2:男
        map.put("sex",19);
        map.put("man",19);
        return "study";
    }
}

3.模板实现

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

     <!--th:if 如果值为true,则显示标签-->
     <h3 th:if="${#lists.isEmpty(userList)}">userList是空的</h3>
     <h3 th:if="not ${#lists.isEmpty(userList)}">userList不是空的</h3>
     <h3 th:unless=" ${#lists.isEmpty(userList)}">userList不是空的</h3>
     <hr />
     <div th:switch="${sex}">
         <p th:case="1"> 女</p>
         <p th:case="${man}"> 男</p>
         <!--若上面条件均不满足-->
         <p th:case="*"> 未知</p>
     </div>
</body>
</html>

关键-》(th:if   th:unless)  (th:switch  th:case) 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值