Web基础(五)----JSTL技术

一、JSTL概述

JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同el一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库,但随着发展,目前常使用的是他的核心库


二、JSTL下载与导入

JSTL下载:
从Apache的网站下载JSTL的JAR包。进入 “http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/”网址下载JSTL的安装包。jakarta-taglibs-standard-1.1.2.zip,然后将下载好的JSTL安装包进行解压,此时,在lib目录下可以看到两个JAR文件,分别为jstl.jar和standard.jar。其中,jstl.jar文件包含JSTL规范中定义的接口和相关类,standard.jar文件包含用于实现JSTL的.class文件以及JSTL中5个标签库描述符文件(TLD)


将两个jar包导入我们工程的lib中
使用jsp的taglib指令导入核心标签库


三、JSTL核心库的常用标签

1)<c:if test=””>标签
其中test是返回boolean的条件
2)<c:forEach>标签
使用方式有两种组合形式:
<%@ page language="java" contentType="text/html; charset=UTF-16"
	pageEncoding="UTF-16"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-16">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setAttribute("count", 10);
	%>


	<!-- jstl经常会和el配合使用 -->
	<!-- test代表的返回Boolean的表达式 -->
	<c:if test="${count==10}">
		xxx
	</c:if>
	<c:if test="${count!=10}">
		yyy
	</c:if>
	
	<!-- forEach模拟
		for(int i=0; i<=5; i++){
			System.out.println(i);
		}
	 -->
	 <c:forEach begin="0" end="5" var="i">
	 	${i }<br>
	 </c:forEach>	
	 
	 <!-- 模拟增强for
	 	for(Product product : products){
	 		syso(product.getPname());
	 	}
	  -->
	 <!-- items:一个集合或数组  var:代表集合中的某一个元素 -->
	 <c:forEach items="${productList }" var="pro">
	 	${pro.pname}
	 </c:forEach>
</body>
</html>
运行效果:


<%@ page language="java" contentType="text/html; charset=UTF-16"
	pageEncoding="UTF-16"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-16">
<title>Insert title here</title>
</head>
<body>
	<%
		//模拟List<String> strList
		List<String> strList = new ArrayList<String>();
		strList.add("java");
		strList.add("c#");
		strList.add("python");
		request.setAttribute("strList", strList);
	%>
	<h1>取出strList的数据</h1>
	<c:forEach items="${strList }" var="str">
		${str }<br>
	</c:forEach>
</body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值