Thymeleaf常见用法

Thymeleaf下拉框回显:

<div class="form-group draggable">
    <label class="col-sm-3 control-label">bizMch:</label>
    <div class="col-sm-8">
        <select class="form-control" name="bizMchId" id="bizMchId">
            <option  th:each="bizMch:${list}" th:selected="${bizMch.bizMchId==homeMap.bizMchId}"
                     th:value="${bizMch.bizMchId}" th:text="${bizMch.name}"
                     ></option>
        </select>
    </div>
</div>


下拉框(session版):

<div class="form-group draggable">
   <label class="col-sm-3 control-label">bizMch:</label>
   <div class="col-sm-8">
      <select class="form-control" name="bizMchId" id="bizMchId">
            <option  th:each="list:${session.list}" th:value="${list.bizMchId}" th:text="${list.name }"></option>
      </select>
   </div>
</div>

1.thymeleaf 标签获取 contextPath:

<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    contextPath = /*[[@{/}]]*/ '';
    /*]]>*/
</script>
2、thymeleaf 循环标签
<tr th:each="info:${list}">
info 为循环变量,在循环中使用info点属性即可

3、thymeleaf  使用 href标签带参数:

<a class="acicon" th:href="@{/attendanceRecord/userAttendanceRecordListPage(userInfoId=${attendanceRecordDTO.userInfoId})}">
4、thymeleaf  中将值传给 事件函数:
<a class="acicon" ><img  th:src="@{/img/common/listedit.png}" th:name="${userInfo.userInfoId}" onclick="editUserInfoPage(this)"/></a>
然后 $(this).attr('name');获取name的值 这个方法有点臃肿 不过能实现

5、循环给select 赋值:

<select  name="areaInfoId" class="selectpicker" onchange="setCity(this)">
    <option>请选择省</option>
    <option th:each="areaInfo : ${areaInfos}" th:value="${areaInfo.areaInfoId}" th:text="${areaInfo.areaName}"></option>
</select>
6、给select动态选中:
<select name="enableFlag" class="selectpicker" onchange="setAreaName()">
    <option value="true" th:if="${userInfo.enableFlag ==true}" th:selected="selected"></option>
    <option value="false" th:if="${userInfo.enableFlag ==false}" th:selected="selected"></option>
    <option value="true" th:if="${userInfo.enableFlag ==false}"></option>
    <option value="false" th:if="${userInfo.enableFlag ==true}"></option>
</select>

7、路径信息:

<script type="text/javascript" th:src="@{/js/basis/jquery/jquery-1.10.2.min.js}"></script>
<link rel="stylesheet" type="text/css" th:href="@{/style/basis/bootstrap/bootstrap3.min.css}"/>

8、字符串拼接:

<td th:text="${info}+'str'"></td>
注意双引号是必须要有的

9、使用thymeleaf控制 单选框

<ul>
    <li class="fl">
        <input type="radio" name="enableFlag"   th:checked="${userInfo.enableFlag== true}" value="true" style="margin: 10px;"/>
        <label class="iRadioLable" style="width:auto; max-width:auto;margin: 0"></label>
    </li>
    <li class="fl">
        <input type="radio" name="enableFlag" th:checked="${userInfo.enableFlag==false}" value="false" style="margin: 10px"/>
        <label class="iRadioLable" style="width:auto; max-width:auto;margin: 0"></label>
    </li>
</ul>

10、thymeleaf中使用三目运算

<td th:text="${imposeOccupyInfo.certificateCode== null} ? '-':${imposeOccupyInfo.certificateCode}"></td>

11、thymeleaf中动态控制class 样式

<li  th:class="${enterpriseInfo.creditGrade gt iterStat.index}?'start':''"></li>

12、thymeleaf中使用时间格式化

<td th:text="${#dates.format(enterpriseInfo.insertTime,'yyyy-MM-dd')}"></td>

13.radio回显

<div class="form-group">    
    <label class="col-sm-3 control-label">settleType:</label>
    <div class="col-sm-8">
        <label class="radio-inline">
            <input th:field="*{merchant.settleType}" type="radio" name="settleType" value="1" checked> open
        </label>
        <label class="radio-inline">
            <input th:field="*{merchant.settleType}" type="radio" name="settleType"  value="2"> close
        </label>
    </div>
</div>

猜你喜欢

转载自blog.csdn.net/qq_36850813/article/details/80599230