JSTL Core详解(二)
<c:out>标签是一个最常用的标签,用于在JSP中显示数据。它的属性和描述如表所示:
value输出到页面的数据,可以是EL表达式或常量(必须)default当value为null时显示的数据(可选)escapeXml当设置为true时会主动更换特殊字符,比如“<,>,&”(可选,默认为true)
<c:out value="${sessionScope.anyValue}" default="no value" escapeXml="false"/>value值的信息,可以是EL表达式或常量target被赋值的JavaBean实例的名称,若存在该属性则必须存在property属性(可选)propertyJavaBean实例的变量属性名称(可选)var被赋值的变量名(可选)scope变量的作用范围,若没有指定,默认为page(可选
<c:set value="this is andy" var="oneString"/>${oneString} <br>var需要被删除的变量名scope变量的作用范围,若没有指定,默认为全部查找(可选)
<c:remove var="sampleValue" scope="session"/>${sessionScope.sampleValue} <br><c:catch var="err"> ${param.sampleSingleValue[9] == 3}</c:catch>${err}test需要判断的条件var保存判断结果true或false的变量名,该变量可供之后的工作使用(可选)scope变量的作用范围,若没有指定,默认为保存于page范围中的变量(可选)
<c:if test="${paramValues.sampleValue[2] == 12}" var="visits">It is 12</c:if><br>${visits} <br><c:choose> <c:when test="${paramValues.sampleValue[2] == 11}"> not 12 not 13,it is 11 </c:when> <c:when test="${paramValues.sampleValue[2] == 12}"> not 11 not 13,it is 12 </c:when> <c:when test="${paramValues.sampleValue[2] == 13}"> not 11 not 12,it is 13 </c:when> <c:otherwise> not 11 、12、13 </c:otherwise></c:choose>items进行循环的集合(可选)begin开始条件(可选)end结束条件(可选)step循环的步长,默认为1(可选)var做循环的对象变量名,若存在items属性,则表示循环集合中对象的变量名(可选)varStatus显示循环状态的变量(可选)
<%ArrayList arrayList = new ArrayList(); arrayList.add("aa"); arrayList.add("bb"); arrayList.add("cc");%><%request.getSession().setAttribute("arrayList", arrayList);%><c:forEach items="${sessionScope.arrayList}" var="arrayListI"> ${arrayListI}</c:forEach><c:forEach var="i" begin="1" end="10" step="1"> ${i}<br /></c:forEach>items进行分隔的EL表达式或常量delims分隔符begin开始条件(可选)end结束条件(可选)step循环的步长,默认为1(可选)var做循环的对象变量名(可选)varStatus显示循环状态的变量(可选)
<c:forTokens items="aa,bb,cc,dd" begin="0" end="2" step="2" delims="," var="aValue"> ${aValue}</c:forTokens>url需要导入页面的URLcontextWeb Context该属性用于在不同的Context下导入页面,当出现context属性时,必须以“/”开头,此时也需要url属性以“/”开头(可选)charEncoding导入页面的字符集(可选)var可以定义导入文本的变量名(可选)scope导入文本的变量名作用范围(可选)varReader接受文本的java.io.Reader类变量名(可选)
<c:import url="/MyHtml.html" var="thisPage" /><c:import url="/MyHtml.html" context=”/sample2” var="thisPage"/><c:import url="www.sample.com/MyHtml.html" var="thisPage"/>
value页面的URL地址contextWeb Context该属性用于得到不同Context下的URL地址,当出现context属性时,必须以“/”开头,此时也需要url属性以“/”开头(可选)charEncodingURL的字符集(可选)var存储URL的变量名(可选)scope变量名作用范围(可选)
<c:url value="/MyHtml.html" var="urlPage" /><a href="${urlPage}">link</a><c:redirect url="/MyHtml.html"/>
name传递的参数名value传递的参数值(可选)