JavaWeb——JSTL标签库

1. 定义

JSTL(JSP Standard Tag Library):JSP标准标签库,JSP为开发者提供的一系列的标签,使用这些标签可以完成一些逻辑处理,比如循环遍历集合,让代码更加简洁,不再出现JSP脚本穿插的情况。
实际开发中EL和JSTL结合起来使用,JSTL侧重于逻辑处理,EL负责展示数据。

2. 使用方法

  1. 需要导入jar包(两个jstl.jar和standard.jar)

存放的位置web/WEB-INF

  1. 在JSP页面开始的地方导入JSTL标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  1. 在需要的地方使用

使用jstl

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="bysj01.User" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>$Title$</title>
</head>
<body>
	<table>
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>成绩</th>
		</tr>
		<%
			List<User> list =(List<User>) request.getAttribute("list");
			for(User user:list){
				request.setAttribute("user", user);
			
		%>
		<tr>
			<td>${user.id}</td>
			<td>${user.name}</td>
			<td>${user.score}</td>
		</tr>
		<%
			}
		%>
		
	</table>
	
</body>
</html>

不使用jstl

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="bysj01.User" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>$Title$</title>
</head>
<body>
	<table>
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>成绩</th>
		</tr>
		<c:forEach items="${list}" var="user">
			<tr>
			<td>${user.id}</td>
			<td>${user.name}</td>
			<td>${user.score}</td>
		</tr>
		</c:forEach>
	</table>
	
</body>
</html>

3. JSTL优点

  • 提供了统一的标签
  • 可以用于编写各种动态功能

4. JSTL核心标签库常用标签

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

4.1 set

向域对象中添加数据
相当于

1.
<%
	request.setAttribute(key,value)
%>

2.
<c:set var="name" value="tom" scope="request"></c:set>
${requestScope.name}

3.
<%
	User user = new User(1,"张三",66.6);
	request.setAttribute("user", user);
%>
${user.name}
<c:set target="${user}" property="name" value="李四"></c:set>
${user.name}

4.2 out

输出域对象中的数据
相当于

<c:set var="name" value="tom"></c:set>
<c:out value="${name}" default="未定义"></c:out>

4.3 remove

删除域对象中的数据

<c:remove var="name" scope="page"/>
<c:out value
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43820008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值