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

来源:互联网 发布:c语言游戏源代码 编辑:程序博客网 时间:2024/06/06 13:10

准备公用页面,供其它页面进行引用: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>>未到账