JSTL标签库
JSTL标签库全称是指 JSP Standard Tag Library
JSP标准标签库。是一个不断完善的开放的源代码的JSP标签库。
EL表达式主要是为了替换jsp中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个jsp页面变得更加简洁。
JSTL由五种不同功能的标签库组成。
在maven中导入jstl标签库的jar包
set标签
set标签可以往域中保存数据
域对象.setAttribute(key,value);
scope 属性设置保存到哪个域
page 表示pageContext域
request 表示reuqest域
session 表示session域
application域表示ServletContext域
保存之前:${requestScope.abc} <br>
<c:set scope="request" var="abc" value="abcValue"/> <br>
保存之后:${requestScope.abc}
if标签
if标签用来做if判断,test属性表示判断的条件(使用EL表达式输出)
<c:if test="${12==12}">
< h1>12等于12
</c:if>
在java中可以使用if-else但是在if标签里只能使用多个if标签来表示if-else
<c:choose> <c:when> <c:otherwise>标签用来做多路判断
跟java中的switch…case…default
<%
request.setAttribute("height",178);
%>
<c:choose>
<c:when test="${requestScope.height>190}">
<h2>小巨人</h2>
</c:when>
<c:when test="${requestScope.height>180}>">
<h2>很高</h2>
</c:when>
<c:when test="${requestScope.height>170}">
<h2>还可以</h2>
</c:when>
<c:otherwise>
<h3>不到170</h3>
</c:otherwise>
</c:choose>
choose里面不能使用html标签
forEach标签
作用:遍历输出使用
- 遍历1到10,输出
<c:forEach begin="1" end="10" var="i">
${i}
</c:forEach>
- 遍历Object数组
<%
request.setAttribute("arr",new String[]{"张三","李四","王五"});
%>
<c:forEach items="${requestScope.arr}" var="item">
${item}
</c:forEach>
- 遍历List集合——list中存放Person类,有属性:编号,用户名,密码,年龄,电话信息
<%
ArrayList<Student> list = new ArrayList<>();
Student stu1 = new Student("张三",18,"男");
Student stu2 = new Student("王五", 11, "女");
Student stu3 = new Student("赵四", 22, "男");
list.add(stu1);
list.add(stu2);
list.add(stu3);
request.setAttribute("list",list);
%>
<c:forEach items="${requestScope.list}" var="stu">
${stu} <br>
</c:forEach>
- 遍历Map集合
<%
HashMap<String, Object> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
request.setAttribute("map",map);
%>
<c:forEach items="${requestScope.map}" var="entry">
${entry} <br>
</c:forEach>