jstl常用语句

1.select框中if选中,下面的语句实现从后台给过来一个category实体,如果category的categoryType为指定的值,则选中。

<select class="form-control" name="categoryType">
        <option value="1"<c:if test='${category.categoryType==1}'>selected</c:if>>材料</option>
        <option value="2"<c:if test='${category.categoryType==2}'>selected</c:if>>产品</option>
</select>

 2.多种选择的时候,用when和otherwise,并且,判断空或非空,用empty和not empty

<c:choose>
    <c:when test="${not empty category.categoryPic}">
        <img id="preview" src="./${category.categoryPic}" />
    </c:when>
    <c:otherwise><img id="preview" src="./static/img/nopic.png" /></c:otherwise>
</c:choose>

 3.在select动态获取数据,循环,并判断初始值的方法

1 <select name="partnerLevelId" class="form-control">
2    <OPTION value="">请选择</OPTION>
3       <c:forEach items="${rootCategoryList}" var="rootCategory">
4           <option value="${rootCategory.categoryId}"
5               <c:if test="${rootCategory.rootCategoryId==pm.rootCategoryId}">selected</c:if>>${rootCategory.categoryName}</option>
6       </c:forEach>
7 </select>

 4.list中按索引获取元素

<button type="button" class="btn btn-xs bg-maroon gift_add" partner_level_id='${partnerLevelList[2].partnerLevelId}'>Add</button>

5.循环并显示序号

<c:forEach items="${giftList1}" var="gift" varStatus="status">
    <tr>
        <td>${status.index+1}.</td>
        <td>${gift.giftName}</td>
        <td><span class="badge bg-yellow"><a href="#">edit</a></span></td>
    </tr>
</c:forEach>

 5.c:if也可以用于判断,但是需要注意的是,没有c:else。

<c:if test="${admin.roleId==role.roleId}">selected</c:if>

 6.按一定的形式显示时间

<fmt:formatDate value="${employee.birthday}" pattern="yyyy-MM-dd HH:mm:ss"/>
原文地址:https://www.cnblogs.com/roy-blog/p/7063628.html