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>
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>