1. 程式人生 > >jsp頁面常用標籤

jsp頁面常用標籤

以下是日常開發中jsp經常使用的標籤

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions"  prefix="fn"%>

1.<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>是c標籤

2.<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>是日期格式化

3.<%@ taglib uri="http://java.sun.com/jsp/jstl/functions"  prefix="fn"%>是獲得集合資料的長度

案例:

c標籤c:foreach迴圈list資料(後臺直接返回list資料即可)如:

// 根據商品ID查詢單個的商品物件
 GoodsMoifiedVo goodsMoifiedVo = this.goodsService.queryModeifiedVoByGoodsId(goodsId);

//設定商品資訊
mav.addObject("goodsMoifiedVo", goodsMoifiedVo);

select選中

<div class="add_good_item_cont">
	<span class="label_text"><span class="text-danger">*</span>商品品牌:</span>
	<div class="add_good_form">
		<div class="w500">
		<div class="input-group">
			<select class="form-control inline required" data-live-search="true" name="brandId" id="goods_brand">
                             <c:forEach items="${brandList }" var="brand">
                                   <option value="${brand.brandId }" <c:if test="${goodsMoifiedVo.goodsBrand.brandId==brand.brandId}">selected="selected" </c:if>>${brand.brandName }</option>
                                   </c:forEach>
                        </select>
		</div>
	</div>
     </div>
</div>

checkbox選中

<div class="add_good_item_cont">
    <span class="label_text">商品標籤:</span>
    <div class="add_good_form">
	<div class="w500 inline_block">
	    <c:forEach items="${tagList }" var="tag">
                <label class="checkbox-inline">
                  <input type="checkbox" name="update_goods_tags" style="margin-top:3px;" class="goods_tag" value="${tag.tagId }"
                     <c:forEach items="${goodsMoifiedVo.tags }" var="goodsTag">
                           <c:if test="${tag.tagId==goodsTag.goodsTag.tagId}">checked</c:if>
                     </c:forEach>
                      >${tag.tagName }
                 </label>
           </c:forEach>
	</div>
   </div>
</div>
c標籤獲得list資料長度:products是後臺返回的list資料名稱如:

//根據商品id查詢貨品資訊
List<GoodsProductVo> products = this.goodsProductService.queryProductListByGoodsId(goodsId);

//設定貨品資訊
mav.addObject("products", products);

<!-- 貨品個數 -->
<input type="hidden" id="productSize" value="${fn:length(products)}"/>

c標籤c:if判斷
<div class="form-group">
    <div class="input-group">
	<span class="input-group-addon">預設店鋪</span>
	<select class="form-control" name="isDefaultStore">
	   <option value="" <c:if test="${empty pageBean.objectBean.isDefaultStore }">selected="selected"</c:if> >請選擇</option>
	   <option value="1" <c:if test="${not empty pageBean.objectBean.isDefaultStore && pageBean.objectBean.isDefaultStore=='1' }">selected="selected"</c:if> >是</option>
	   <option value="0" <c:if test="${not empty pageBean.objectBean.isDefaultStore && pageBean.objectBean.isDefaultStore=='0' }">selected="selected"</c:if> >否</option>
	</select>
   </div>
</div>

<c:forEach items="${helpCates}" var="helpcate"  varStatus="status">
    <c:if test="${status.index>=0 && status.index<5}">
	<dl class="svc_box svc_01 pull-left" style="background-image:none;width:210px;">
	     <dt>${helpcate.helpcateName}</dt>
	     <dd>
		 <c:forEach items="${helpcate.helpCenters}" var="helpcenter"  varStatus="sta">
		      <c:if test="${sta.index>=0 && sta.index<6 }">
		            <c:if test="${helpcenter.isFoot=='1' }">
		                <div>
		                <a href="javascript:;" target="_blank">${helpcenter.helpTitle}</a></div>
		            </c:if>
		     </c:if>
		 </c:forEach>
	     </dd>
	</dl>
    </c:if>
</c:forEach>

注意以上:

items:表示要遍歷集合的名稱,如後臺返回的是mav.addOjbect("helpCates",getHelpCateList())

var:給遍歷集合取個變數名稱任意(見名知義)

varStatus:迭代變數的名稱,用來表示迭代的狀態,可以訪問到迭代自身的資訊,常用的屬性為:current,index,count。

current:當前這次迭代的(集合中的)項。
index:當前這次迭代從0開始的迭代索引。
count:當前這次迭代從1開始的迭代計數

案例:以下如何顯示資料序號,用法:

<c:forEach items="${pageBean.data }" var="ossConf"  varStatus="status">
      <tr class=" <c:if test="${ossConf.ossStatus eq 0 }">disabled</c:if> ">
        <td>
            ${status.count}
        </td>


empty,not empty判斷某個元素值是否為空