备忘:现有项目JSTL使用汇总
2017-08-29 00:00
218 查看
准备公用页面,供其它页面进行引用: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为true或false <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>>未到账
相关文章推荐
- 备忘:现有项目JSTL使用汇总
- 项目期间..简单备忘...JSTL之读取集合Map
- 在使用JSTL时要往项目里导的jstl.jar和standard,jar在哪里找?
- JSTL使用备忘
- 使用现有库与头文件在visual studio环境下创建新项目
- 使用EGit插件将Eclipse现有项目分享到git@osc
- 使用Prism重构项目中的问题汇总
- 项目中经常使用的JS方法汇总,非常有用
- Maven项目下使用JSTL需导入的依赖
- 在现有项目中使用AspNet Identity 2.0 实战
- maven 项目中使用 jstl标签
- Web项目中使用JSTL标签问题
- node.js项目中使用coffeescript的方式汇总
- GitHub使用--Windows 7 将一个现有项目添加到新建远程仓库
- 项目期间..简单备忘..JSTL之格式化输出
- Maven项目下使用JSTL需导入的依赖
- JSTL表达式在项目中的使用
- 项目期间,简单备忘...JSTL之if
- 添加现有项目来重用代码。使用编译功能查找错误。
- 【备忘】在类库项目中使用 Razor 智能感知