JSP
JSP标准标签库
JSP标准标签库(JSTL):封装了JSP应用的通用核心功能;支持通用的、结构化的任务;比如迭代;条件判断;XML文档操作;国际化标签;SQL标签;
####核心标签
<c:out> #用来显示一个表达式的结果;可自动忽略XML标记字符;
<c:set> #用于设置变量值和对象属性;会计算表达式的值;
<c:if> #同if;
<c:choose> #只当做<c:when>和<c:otherwise>的父标签;
<c:when> #条件判断;<c:otherwise> 接在<c:when>标签后;当<c:when>判断全为false时被执行;
<c:forTokens> #标签通过指定分隔符将字符串分隔为一个数组然后迭代它们。
<c:forEach> #基础迭代标签;接受多种集合类型;
item #data
begin #开始索引;0
end #结束索引;Last element
step #步伐;1
var #当前变量名称
varStatus #代表当前循环状态的变量名称;first;last;begin;end;step;count(迭代计数,从1开始);index(迭代索引)
####函数
fn:join();fn:split()
fn:length();fn:indexOf()
fn:replace();fn:substring()
fn:toLowerCase();fn:toUpperCase();fn:trim()
fn:startsWith();fn:endsWith();fn:contains();fn:containsIgnoreCase()
fn:substringAfter() #返回字符串在指定子串之后的子集;
fn:substringBefore() #返回字符串在指定子串之前的子集;
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
//escapeXml:是否忽略XML特殊字符
<c:out value="<要显示的数据对象(未使用转义字符)>" escapeXml="true" default="默认值"></c:out>//<要显示的数据对象(未使用转义字符)>
<c:out value="<要显示的数据对象(使用转义字符)>" escapeXml="false" default="默认值"></c:out>//<要显示的数据对象(使用转义字符)>
<c:out value="${null}" escapeXml="false">使用的表达式结果为null;则输出该默认值</c:out>//使用的表达式结果为null;则输出该默认值
//scope:var属性的作用域
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:out value="${salary}"/>
//无else
<c:if test="${index==0}">
<p>${index}<p>
</c:if>
<c:choose>
<c:when test="${index==0}"></c:when>
<c:when test="${index>0}"></c:when>
<c:otherwise></c:otherwise>
</c:choose>
//<c:forEach var="i" begin="1" end="5">
//<c:forEach var="r" items="${quesList}" begin="1" end="10" step="3" varStatus="s">
<c:forEach var="r" items="${quesList}" varStatus="s">
${userStatus.first}
</c:forEach>
<c:forTokens items="google,runoob,taobao" delims="," var="name">
<c:out value="${name}"/><p>
</c:forTokens>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
//${fn:length(collection | string)}
<td rowspan="${fn:length(rr.children)}">${rr.name}</td>
//${fn:indexOf(<原始字符串>,<子字符串>)}
//${fn:split(<待分隔的字符串>, <分隔符>)}
//${fn:join([数组], <分隔符>)}
//${fn:replace(<原始字符串>, <被替换的字符串>, <要替换的字符串>)}
//${fn:substring(<string>, <beginIndex>, <endIndex>)}
//${fn.toLowerCase(<string>)}
//${fn:trim(<string>)}
//${fn:startsWith(<原始字符串>, <搜索的前缀>)}
//${fn:contains(<原始字符串>, <要查找的子字符串>)}
//${fn:substringAfter(<string>, <substring>)}
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substringBefore(string1, 'first')}" /> //This is
<c:set var="string2" value="${fn:substringAfter(string1, 'is')}" /> //is first String.