JavaWeb学习笔记——JSTL标签库

介绍

JSTL 标签库,JSP Standard Tag Library,JSP 标准标签库
EL 表达式替换了 jsp 页面的表达式脚本,JSTL 标签库则替换了 jsp 页面的代码脚本,这样使得整个 jsp 页面
变得更佳简洁。

标签库

JSTL 由五个不同功能的标签库组成。
在这里插入图片描述

使用方法

  1. 导入 JSTL 标签库的 jar 包。
    taglibs-standard-impl-1.2.1.jar
    taglibs-standard-spec-1.2.1.jar
  2. 使用 taglib 指令在 jsp 页面中导入标签库。
    在这里插入图片描述

core 标签库

<c:set />(使用很少)

作用:向域中存储数据

  • scope属性: 设置保存到哪个域
             page:表示PageContext域(默认值)
             request:表示Request域
             session:表示Session域
             application:表示ServletContext域
  • var属性:设置key
  • value属性:设置值
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%-- 
		set标签:<c:set>
			作用:向域中存储数据
			scope属性: 设置保存到哪个域
				page:表示PageContext域(默认值)
				request:表示Request域
				session:表示Session域
				application:表示ServletContext域
			var属性:设置key
			value属性:设置值
	 --%>
	 <c:set scope="request" var="a" value="123"></c:set>
	 ${ requestScope.a }

</body>
</html>

<c:if />

作用:进行 if 判断
test属性:表示判断的条件(使用 EL表达式)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	 <c:set scope="request" var="a" value="123"></c:set>
	 <%-- 
	 	if标签:<c:if>
	 		作用:进行if判断
	 		test属性:表示判断的条件(使用 EL表达式)
	  --%>
	  <c:if test="${ requestScope.a == 123 }">
	  	<h2>JSTL的if标签</h2>
	  </c:if>
	 
</body>
</html>

<c:choose> <c:when> <c:otherwise>

作用:多路判断,与 switch … case … default 相似

  • choose标签:多路判断的开始标签
  • when标签:表示每一种情况
        test属性:表示当前这种情况要满足的条件
  • otherwise标签:表示剩下的情况

注意:

  1. 标签里不能使用 html 注释,要使用 jsp 注释
  2. when标签的父标签必须是 choose 标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	  <%--
		<c:choose> <c:when> <c:otherwise>标签:
			作用:多路判断,与switch ... case .... default相似
		choose标签:多路判断的开始标签
		when标签:表示每一种情况
			test属性:表示当前这种要满足的条件
		otherwise标签:表示剩下的情况
		
		注意:
			1、标签里不能使用html注释,要使用jsp注释
			2、when标签的父标签必须是choose标签
	  --%>
	  <c:set scope="request" var="height" value="178"></c:set>
	  <c:choose>
	  	<c:when test="${ requestScope.height >= 180 }">
	  		<div>身高大于180cm</div>
	  	</c:when>
	  	<c:when test="${ requestScope.height < 180 && requestScope.height >= 170  }">
	  		<div>身高在170-180cm之间</div>
	  	</c:when> 
	  	<c:otherwise>
	  		<div>身高小于170cm</div>
	  	</c:otherwise>
	  </c:choose>
	  
</body>
</html>

<c:forEach />

常规遍历

  • begin属性:设置开始索引
  • end属性:设置结束索引
  • var属性:表示循环的变量,即当前正在遍历的数据
  • step属性:步长
<body>
	<%--
		<c:forEach>标签 1:
			作用 :遍历——常规遍历
			begin属性:设置开始索引
			end属性:设置结束索引
			var属性:表示循环的变量,即当前正在遍历的数据
			step属性:步长
	--%>
	<%-- 遍历输出1-10 --%>
	<c:forEach begin="1" end="10" var="i">
		<div>${ i }</div><br>
	</c:forEach>
	
	<%-- 输出25列的表格 --%>
	<table border="1">
		<c:forEach begin="1" end="2" var="i">
			<tr>
				<c:forEach begin="1" end="5" var="j">
					<td>第${ i }行第${ j }个单元格</td>
				</c:forEach>
			</tr>
		</c:forEach>
	</table>
</body>

遍历数组

  • item属性:表示遍历的数据源(遍历的数组)
  • var属性:表示循环的变量,即当前正在遍历的数据
<body>
	<%-- 
		<c:forEach>标签 2:
			作用 :遍历——遍历数组
			item属性:表示遍历的数据源(遍历的数组)
			var属性:表示循环的变量,即当前正在遍历的数据
	 --%>
	 <% 
	 	request.setAttribute("arr", new String[]{"123", "456", "789"});
	 %>
	 <c:forEach items="${ requestScope.arr }" var="item">
	 	${ item }<br>
	 </c:forEach>
</body>

遍历Map集合

  • item属性:表示遍历的数据源
  • var属性:表示循环的变量,即当前正在遍历的数据
<body>
	<%-- 
		<c:forEach>标签 3:
			作用 :遍历——遍历Map集合
			item属性:表示遍历的数据源(遍历的数组)
			var属性:表示循环的变量,即当前正在遍历的数据
			遍历Map集合之后得到EntrySet集合,属性key可以获得全部键,属性value可以获得全部值
			(注意:获取Map集合中的某个值时,map.key1获取的是key=key1的value值)
	 --%>
	 <% 
	 	Map<String, Object> map = new HashMap<String, Object>();
		map.put("key1", "value1");
		map.put("key2", "value2");
		map.put("key3", "value3");
		request.setAttribute("map", map);
	 %>
	 <c:forEach items="${ requestScope.map }" var="entry">
	 	键${ entry.key }值${ entry.value }<br>
	 </c:forEach>
</body>

遍历List集合

  • item属性:表示遍历的数据源
  • var属性:表示循环的变量,即当前正在遍历的数据
<body>
	 <%-- 
		<c:forEach>标签 4:
			作用 :遍历——遍历List集合
			item属性:表示遍历的数据源(遍历的数组)
			var属性:表示循环的变量,即当前正在遍历的数据
			
		需求:
			创建10个学生对象,包括id,username,password,age,phone,
			保存到List集合中,之后遍历输出
	 --%>
	 <%
	 	List<Student> stuList = new ArrayList<Student>();
	 	for(int i=1; i<=10; i++){
	 		stuList.add(new Student(i, "username"+i, "password"+i, 18+i, "phone"+i));
	 	}
	 	request.setAttribute("student", stuList);
	 %>
	 <table>
	 	<tr>
		 	<th>编号</th>
		 	<th>姓名</th>
		 	<th>密码</th>
		 	<th>年龄</th>
		 	<th>电话</th>
	 	</tr>
		<c:forEach items="${ requestScope.student }" var="stu">
			<tr>
		 		<td>${ stu.id }</td>
		 		<td>${ stu.username }</td>
		 		<td>${ stu.password }</td>
		 		<td>${ stu.age }</td>
		 		<td>${ stu.phone }</td>
			</tr>
		</c:forEach>ach>
	</table>
</body>

其他

<body>
	<%-- 
	<c:forEach>标签 5:
		varStatus属性:当前遍历到的数据的状态
			getCurrent:获取当前遍历到的元素
			getIndex:获取当前遍历到的元素的索引
			getCount:当前遍历到的元素的个数
			isFirst:判断当前元素是否是第一个元素
			isLast:判断当前元素是否是最后一个元素
			getBegin:获取begin属性值
			getEnd:获取end属性值
			getStep:获取step属性值
 	--%>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值