JSTL的使用:
1、将jstl相关的jar包拷贝到项目的lib目录下
jstl.jar和standard.jar
2、在页面引入想要使用的库
<%@taglib uri=“要使用的库的路径” prefix=“前缀名” %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3、使用标签
<前缀名:标签名 属性名=“属性值”></前缀名:标签名>
4、标签一般通过el表达式操作域对象
if标签:
条件判断
有两种形式
1、有主体内容
<c:if test="判断条件,返回true或false" var="限域变量名,用户接收判断条件的返回值" scope="限域变量名的域范围 page|request|session|application">
// 当满足条件时,执行的代码
</c:if>
2、无主体内容
<c:if test="判断条件,返回true或false" var="限域变量名,用户接收判断条件的返回值" scope="限域变量名的域范围 page|request|session|application">
</c:if>
choose、when、otherwise标签:
相当于Java中的switch
语法:
<c:choose>
<c:when test="返回boolean类型的判断条件">
// 满足条件执行的代码
</c:when>
...
<c:otherwise>
// 当所有的when不执行时执行的代码
</c:otherwise>
</c:choose>
注:
1、choose标签与otherwise标签没有属性,when标签必须设置test属性
2、choose标签中只能包含when标签与otherwise标签,when标签与otherwise标签可以包含其它标签
Illegal "c:if" after "c:otherwise" tag in "c:choose" tag.
3、choose标签中至少包含一个when标签,最多只有一个otherwise标签
Illegal "choose" without child "when" tag
4、所有的when标签必须设置在otherwise标签之前
Illegal "c:when" after "c:otherwise" tag in "c:choose" tag.
5、所有的when标签不执行的情况下才会执行otherwise标签
<c:choose>
<c:when test="${num <60 }">
<h4>你个渣渣!</h4>
</c:when>
<c:when test="${num >= 60 && num < 80 }">
<h4>哎哟不错哦!</h4>
</c:when>
<c:otherwise>
<h4>你很棒棒哦!</h4>
</c:otherwise>
</c:choose>
forEach标签:
forEach标签
循环内容多次
语法:
<c:forEach begin="开始循环的数" end="结束循环的饿数" var="当前成员的限域变量名" step="循环的间隔数">
</c:forEach>
迭代集合
<c:forEach items="${li}" var="item" begin="0" end="9" varStatus="idx">
</c:forEach>
formatNumber标签:
formatNumber标签
将数值型的数据转换成指定格式的字符串
有主体内容
<fmt:formatNumber type="" var="" >
主体内容
</fmt:formatNumber>
无主体内容:
<fmt:formatNumber type="" var="" value="" />
常用属性
type:要格式化成的类型
number数值型,默认值
percent百分比类型
currency货币型
var:限域变量名,用来接收格化式后的结果,如果不设置该属性,则结果会直接输出;如果设置了,则需要通过el表达式输出限域变量名的值
value:要格式化的数据,与有主体内容的格式主体相同
formatDate标签:
formatDate标签
将日期型的数据转换成指定格式的字符串
<fmt:formatDate value="" var="" type="" dateStyle="" timeStyle="" pattern=""/>
常用属性
value:要格式化的数据
type:要格式化成的类型
日期型(默认)、时间型、日期+时间型
dateStyle:日期型的格式
timeStyle:时间型的格式
pattern:具体的格式
parseNumber标签:
parseNumber标签
将指定格式的字符串转换成数值型数据
type:当前字符串的格式
number数值型,默认值
percent百分比类型
currency货币型
parseDate标签:
parseDate标签
将指定格式的字符串转换成日期型
type:当前字符串的格式
date、time、both
pattern:与当前字符串对应的格式