Thymeleaf th:each遍历,th:if、th:switch 条件判断,input,select,radio 回显赋值

1. th:each 遍历
<!-- 遍历集合,如果被遍历的变量 userList 为 null 或者不存在,则不会进行遍历,也不报错-->
    <tr th:each="user : ${userList}">
        <!-- 将用户的主键 uId 存在在 name 属性中-->
        <td th:text="${user.uId}" th:name="${user.uId}"></td>
        <td th:text="${user.uName}"></td>
        <!-- 使用dates对象格式化日期-->
        <td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd HH:mm')}"></td>
        <!-- 三运运算判断是否已婚-->
        <td th:text="${user.isMarry}?'是':'否'"></td>
        <td th:text="${user.price}"></td>
    </tr>

2. 遍历下拉框
<select class="form-control" style="width: 200px;font-size: 16px" id="stationSelect">
	<option>全区</option>
	<!--后台会传值:model.addAttribute("stationList", stationList); 下拉框所有选项的值-->
	<!--后台会传值:model.addAttribute("stationName", stationName); 用户当前选中的下拉框的值-->
	 <option th:each="station : ${stationList}" th:text="${station.name}" th:selected="${stationName} eq ${station.name}">横岗</option>
</select>
3. th:if 条件判断 
<!--if属性结果为 true,模板会进行显示-->
<p th:if="true">th:if="true"</p>
<!--if属性结果为 false,模板不会进行显示-->
<p th:if="false">th:if="false"</p>
 <!--后台控制器传出数据:model.addAttribute("isMarry", true);-->
<p th:if="${isMarry}">已婚</p>

4:input 回显赋值
<input type="text" class="form-control1" id="name" name="name" th:value="${user.name}">

5:select 回显赋值
<label  class="col-sm-2 control-label">类别</label>
<div class="col-sm-8">
    <select  name="cId" id="cId" class="form-control1" >
        <option value="1" th:field="*{book.cId}">目录1</option>
        <option value="2" th:field="*{book.cId}">目录2</option>
        <option value="3" th:field="*{book.cId}">目录3</option>
        <option value="4" th:field="*{book.cId}">目录4</option>
    </select>
</div>

6:radio 回显赋值
<label for="isBoy" class="col-sm-2 control-label">是否男生</label>
  <div id="isBoy" class="col-sm-8">
     <div class="radio-inline">
       <label><input name="isBoy" type="radio" value="1"  th:field="*{book.isBoy}"/> 否</label>
     </div>
     <div class="radio-inline">
        <label><input name="isBoy" type="radio" value="0"  th:field="*{book.isBoy}"/> 是</label>
      </div>
  </div>

 

Guess you like

Origin blog.csdn.net/JavaAlpha/article/details/103508132