备忘:现有项目JSTL使用汇总

准备公用页面,供其它页面进行引用:tablibs.jsp

<%@ 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"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

JSP页面调用方式

<%@ include file="/taglibs.jsp"%>

用法

设置变量标签

<c:set var="ctx" value="${pageContext.request.contextPath}"/>

条件标签

//字符串比较
<c:if test="${xxx eq '1'}">selected="selected"</c:if>
<c:if test="${aaa ne '3'}">selected="selected"</c:if>

//数值比较
<c:if test="${xxx==0}">selected="selected"</c:if>
<c:if test="${xxx!=0}">selected="selected"</c:if>
<c:if test="${xxx>0}">selected="selected"</c:if>
<c:if test="${xxx<0}">selected="selected"</c:if>
<c:if test="${xxx>=0}">selected="selected"</c:if>
<c:if test="${xxx<=0}">selected="selected"</c:if>

//数值计算
<c:if test="${index%2==0}">selected="selected"</c:if>
<c:if test="${money/10000>=10000}">selected="selected"</c:if>
<c:if test="${aaa-bbb>0}">selected="selected"</c:if>

//变量比较
<c:if test="${aaa eq bbb }"> selected="selected"</c:if>
<c:if test="${aaa ne bbb }"> selected="selected"</c:if>

//多条件
<c:if test="${empty xxxList || xxxList.length eq '1' }">selected="selected"</c:if>
<c:if test="${aaa && bbb eq '1' && ccc ne '2'}">selected="selected"</c:if>

//boolean值,xxx为truefalse
<c:if test="${xxx}">selected="selected"</c:if>

//是否不为空,用于list
<c:if test="${!empty xxxList}">selected="selected"</c:if>
<c:if test="${!empty aa.xxxList}">selected="selected"</c:if>

//是否为空,用于list
<c:if test="${empty xxxList}">selected="selected"</c:if>
<c:if test="${empty aa.xxxList}">selected="selected"</c:if>

迭代标签

<c:forEach items="${xxxList}" var="record" varStatus="status">
    //xxxList:系统返回的列表
    //record:本次循环中定义的变量
    //status:列表状态
    ${status.count}//列表计数
</c:forEach>

<c:forEach var="img" items="${fn:split(xxx,',')}">
    //使用fn.split将xxx用“,”分隔后做为list,供forEach使用
</c:forEach>

选择标签

<c:choose>
    <c:when test="条件1">
        //test的用法同if test
    </c:when>
    <c:when test="条件2">

    </c:when>
    .....
    <c:when test="条件N">

    </c:when>
    <c:otherwise>
        //条件都不满足时
    </c:otherwise>
</c:choose>

格式化标签

//数值格式化
<fmt:formatNumber value="${money}" pattern="#0.00"></fmt:formatNumber>
<fmt:formatNumber value="${money}" pattern="###0.00"></fmt:formatNumber>

//时间格式化
<fmt:formatDate var="nowStr" value="${nowDate}" pattern="yyyy/MM/dd HH:mm:ss"/>

//时间字符串解析
<fmt:parseDate var="create_date" value="${nowDate}" pattern="yyyy-MM-dd HH:mm:ss"/>

字符串处理函数

注意函数标签的位置

//求长度
<c:if test="${fn:length(content) > 20}"></c:if>

//分隔
<c:forEach items="${fn:split(content,',')}" var="rd">
</c:forEach>

//字符串截取
${fn:substring(content,0,10)}

实例

下拉菜单

<select name="status">
    <option></option>
    <option value="1" <c:if test="${status eq '1'}">selected="selected"</c:if> >已到账</option>
    <option value="2" <c:if test="${status eq '2'}">selected="selected"</c:if> >未到账</option>
    <c:forEach var="record" items="list" varStatus="st">
        <option value="${record.key}" <c:if test="${record.key eq '2'}">selected="selected"</c:if> >${record.value}</option>
    </c:forEach>
</select>

Input Radio

<input type="radio" value="1" name="status" <c:if test="${status eq '1' }">checked="checked"</c:if>>已到账
<input type="radio" value="2" name="status" <c:if test="${status eq '2' }">checked="checked"</c:if>>未到账
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值