天天看點

thymeleaf常用屬性--包括th:each,條件判斷和th:inline

1.基礎屬性

<body>
<h1>text屬性:</h1>
<h1 th:text="${lff.name}"></h1><br>

<p>href屬性:</p>
<a th:href="@{/href}">檢視</a><br>

<p>action屬性</p>
<form th:action="@{/action}" th:method="post">
    <button>送出</button>
</form><br>

<p>th:onclick屬性</p>
<a th:onclick="'show('+${lff.id}+')'">點選</a>
<script type="text/javascript">
    function show(id) {
        alert("使用者編号為:" + id);
    }
</script>
<br>
</body>
           
thymeleaf常用屬性--包括th:each,條件判斷和th:inline

2.th:each:常用來進行周遊

@RequestMapping(value = "/each/list")
    public String eachList(Model model){
        List<user> userList=new ArrayList<user>();
        for (int i=0;i<10;i++) {
            User user=new User();
            user.setId(i);
            user.setName("張"+i+"狗");
            user.setPhone("1830000"+i);
            user.setAddress("第"+i+"組");
            userList.add(user);
        }
        model.addAttribute("userList",userList);
        return "eachList";
    }
           
<h1>th:each屬性</h1>
<p>循環周遊List集合</p>
<div th:each="user,userStat:${userList}">
<!--th:each="user, iterStat : ${userlist}"中的 ${userList} 是背景傳過來的集合
◼ user:${userList}集合中的一個資料
◼ iterStat :${userList} 循環體的資訊-->
    <span th:text="${user.id}"></span><br>
    <span th:text="${user.name}"></span><br>
    <span th:text="${user.phone}"></span><br>
    <span th:text="${user.address}"></span><br>
    <br>
</div>
           

結果:

thymeleaf常用屬性--包括th:each,條件判斷和th:inline

3.條件判斷

th:if

th:unless(和判斷條件相反)

th:switch和th:case

@RequestMapping(value = "/condition")
    public String condition(Model model){
        user u1=new user(1,"zjc","183","杭州");
        user u2=new user(2,"lff","182","杭州");
        user u3=new user(3,"張二狗","183","遼陽");
        model.addAttribute("u1",u1);
        model.addAttribute("u2",u2);
        model.addAttribute("u3",u3);
        return "condition";
    }
           
<span th:if="${u1.id eq 1}">
    <p>if判斷成功</p>
</span>

<span th:unless="${u1 eq null}">
    <p>unless判斷成功</p>
</span>

<span th:switch="${u1.id}">
    <span th:case="1">姓名:張家程</span>
    <span th:case="2">姓名:李芳芳</span>
    <span th:case="3">姓名:張二狗</span>
</span>

           

4.th:inline