jap篇 之 JSTL标签库

时间:2022-10-14 18:30:05

JSTL标签库:

JSTL: JSP Standard Tag Library

作用:和【EL配合】使用,可以让用户【尽可能少的使用java源码】。

1,导入jar包

导入(复制粘贴到项目中的lib目录)jstl的jar包。 jstl.jar  standard.jar

2,把JSTL标签库导入到jsp页面中

使用jsp中的taglib指令,将标签库引入:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

注意:加前缀避免与其他人写的标签重名。

prefix="c"相当于给这个标签库起一个别名,将来在页面中就是用以c开头的标签来使用标签库中的标签。这个别名也可以叫其他的名字。

<c:forEach>标签

循环遍历操作,【使用率最高】。

使用<c:forEach>标签 【完整代码】 遍历

<c:forEach var="stu" items="${list }" begin="0"  end="3" varStatus="status" step="1">

varStatus表示循环的状态,用status表示。

step是每次跳过几个,默认值为1,逐个遍历。

Items:遍历的对象

Var:遍历得到的结果名

一般遍历只要写var和items来个属性即可。

【遍历List集合】:

<table>

<c:forEach items="${list }" var="stu">

<tr>

<td>${stu.id }</td>

<td>${stu.name }</td>

<td>${stu.age }</td>

</tr>

</c:forEach>

</table>

【遍历Map集合】:

<c:forEach items="${map }" var="entry">

${entry.key }-->${entry.value.id }

${entry.value.name }   ${entry.value.age }<br>

</c:forEach>

<c:if>标签:

<c:if test="${score>85 }">

......

</c:if>

这样写相当于:

if(score>85){

...

}

<c:choose>标签

<c:when>标签

<c:otherwise>标签

<c:choose>

<c:when test="${score>=90 }">优</c:when>

<c:when test="${score>=80 }">良</c:when>

<c:when test="${score>=70 }">中</c:when>

<c:when test="${score>=60 }">及格</c:when>

<c:otherwise>差</c:otherwise>

</c:choose>

相当于:

if(score>=90){

}else if(score>=80){

}else if(score>=70){

}eles if(score>=60){

}else{

}