国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Thymeleaf系列五 迭代,if,switch語法

1. 概述

這里介紹thymeleaf的編程語法,本節(jié)主要包括如下內(nèi)容

  1. 迭代語法:th:each; iteration status
  2. 條件語法:th:if; th:unless
  3. switch語法:th:switch; th:case; *

下文演示以上語法的用法。

2. 演示以上語法的用法

2.1. 公共類

User

public class User {    private String name;    private boolean isAdmin;    private String other;    private int age;    public User(String name, boolean isAdmin, String other, int age) {        super();        this.name = name;        this.isAdmin = isAdmin;        this.other = other;        this.age = age;    }    // set/get略}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

ProgrammingCtl : control類

@Controller@RequestMapping("/programming")public class ProgrammingCtl {    @RequestMapping("programming")    public String iteration(ModelMap modeMap) {        // Iteration        List<User> userList = new ArrayList<User>();        userList.add(new User("son_1", true, "other_1", 11));        userList.add(new User("son_2", false, "other_2", 22));        userList.add(new User("son_3", true, "other_3", 33));        userList.add(new User("son_4", false, "other_4", 44));        modeMap.put("userList", userList);        // ifelse        User userIf = new User("admin", true, "other_if", 11);        modeMap.put("user", userIf);        return "programming/programming";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

本請求轉(zhuǎn)到頁面programming.html,

2.2. 迭代語法:th:each; iteration status

常用th:each用法:

<table border="2">    <thead>         <tr>            <th>name</th>            <th>age</th>            <th>isAdmin</th>         </tr>    </thead>    <tbody>        <!-- 常用的迭代 th:each 用法 -->        <tr th:each="user : ${userList}">            <td th:text="${user.name}"></td>            <td th:text="${user.age}"></td>            <td th:text="${user.isAdmin}"></td>        </tr>    </tbody></table>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

運行結(jié)果如下:

迭代的對象
本例子中迭代的對象是Java.util.List,除了List,還可以對以下對象進行迭代

  • java.util.Iterable
  • java.util.Enumeration
  • java.util.Iterator
  • java.util.Map,此時迭代返回的對象是java.util.Map.Entry
  • 數(shù)組

獲取迭代的中間的狀態(tài),定義在iterStat中

在迭代過程中,可以獲取迭代的中間狀態(tài),詳細如下:

  • index :當前節(jié)點的索引,從0開始
  • size : 迭代節(jié)點總數(shù)
  • even/odd:當前是偶數(shù)/奇數(shù)行,boolean值
  • first/last:當前是每天/最后一個元素
<table border="2">    <thead>         <tr>            <th>迭代索引</th>            <th>元素所處的位置索引</th>            <th>奇偶行</th>            <th>name</th>            <th>age</th>            <th>isAdmin</th>         </tr>    </thead>    <tbody>        <!-- 獲取迭代的中間的狀態(tài),定義在iterStat中-->        <tr th:each="user,iterStat : ${userList}">            <!-- index: 當前迭代的索引 -->            <td th:text="${iterStat.index }"></td>            <!-- first: 當前元素是第一個元素; last: 當前元素是最后個元素 -->            <td th:text="${iterStat.first } ? '這是第一個元素':(${iterStat.last} ? '這是最后一個元素':'')" ></td>            <!--  -->            <td th:text="${iterStat.odd} ? 'odd' : 'even'" ></td>            <td th:text="${user.name}"></td>            <td th:text="${user.age}"></td>            <td th:text="${user.isAdmin}"></td>        </tr>    </tbody></table>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

運行結(jié)果如下:

2.3. 條件語法:th:if; th:unless

演示如下功能

  • th:if:如果值是true,則打印整個節(jié)點
  • th:unless: 和th:if是相反功能,如果值為false,則打印整個節(jié)點
    <!-- th:if:如果值是true,則打印<span>整個節(jié)點  -->    <span th:if="${user.isAdmin}" th:text="${user.name} + '是管理員'">  </span><br />    <!-- th:unless: 和th:if是相反功能,如果值為false,則打印<span>整個節(jié)點  -->    <span th:unless="not ${user.isAdmin}" th:text="${user.name} + '是管理員'">  </span><br />
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

輸出:

<span>admin是管理員</span><br /><span>admin是管理員</span><br />
  • 1
  • 2
  • 1
  • 2

th:if條件判斷
除了判斷boolean值外,thymeleaf還認為如下表達式為true:

  • 值非空
  • 值是character,但是非0
  • 值是非0數(shù)字
  • 值是字符串,但是不是 “false”, “off” or “no”
  • 值不是boolean值,數(shù)字,character 或 字符串

2.4. switch語法:th:switch; th:case; *

演示如下功能

  • th:switch / th:case
  • th:case=”*” : 類似switch中的default
<!-- th:switch / th:case --><div th:switch="${user.name}">  <p th:case="'admin'">User is an administrator</p>  <!-- *: case的默認的選項 -->  <p th:case="*">User is some other thing</p></div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

輸出:

<div>  <p>User is an administrator</p></div>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

3. 代碼

代碼詳細見Github

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Scala 和 XML
如何讓Administrator賬戶不出現(xiàn)在Win7的登陸界面?
js模板引擎
2015-2016高考英語語法填空題專項訓練85篇
英語語法:基數(shù)詞和序數(shù)詞1-19
[神馬語法]初中版:100個句子輕松搞定初中英語語法-第6句
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服