JSP中JSTL核心标签库的<c:if>标签

本文介绍JSP中JSTL核心标签库的c:if标签使用方法,包括无体和有体两种形式,并通过示例代码展示了如何进行条件判断及属性设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JSP中JSTL核心标签库的<c:if>标签
c:if标签的两种形式如下:
1.无体:
<c:if test="testCondition" [var="varName"] [scope="page|request|session|application"] />
2.有体:
<c:if test="testCondition" [var="varName"] [scope="page|request|session|application"] />
Content
</c:if>

test中执行判断,结果为true时执行Content中的内容,var保存test中的布尔值,scope设置属性范围


testJSTLlogin.jsp
view plaincopy to clipboardprint?
1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7. <title>TestJSTLlogin</title>
8. </head>
9. <body>
10. <c:set var="name" value="mgc" scope="session" />
11. <a href="testJSTLWelcome.jsp">登录</a>
12. </body>
13. </html>


testJSTLWelcome.jsp
view plaincopy to clipboardprint?
1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7. <title>TestJSTLlogin</title>
8. </head>
9. <body>
10. <c:if test="${empty sessionScope.name }">
11. <c:redirect url="testJSTLlogin.jsp" />
12. </c:if>
13. 登录成功!
14. </body>
15. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值