小峰servlet/jsp(6)jstl核心标签库

一、引入jstl

需要jstl.jar;standard.jar;

二、jstl核心标签库:

c:out               内容输出标签;

c:set             用来设置4种属性范围值的标签;

c:remove         用来删除指定范围中的属性;

c:catch        用来处理程序中产生的异常;

c:if          用来条件判断;

c:choose/c:when/c:otherwise 用来多条件判断;

c:forEach        用来遍历数组或者集合;

c:fortokens      分隔输出;

c:import         导入页面;

c:url           生成一个url地址;

c:redirect        客户端跳转

c:out:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <%
12     pageContext.setAttribute("people","张三");
13 %>
14 <h2><c:out value="${people}"></c:out></h2>
15 <h2><c:out value="${people2}" default="某人"></c:out></h2>
16 </body>
17 </html>
View Code

c:set

 1 <body>
 2 <c:set var="people" value="张三" scope="request"></c:set>
 3 <h2><c:out value="${people}"></c:out></h2>
 4 <jsp:useBean id="people2" class="com.java1234.model.People" scope="page"></jsp:useBean>
 5 <c:set property="id" target="${people2 }" value="007"></c:set>
 6 <c:set property="name" target="${people2 }" value="王二小"></c:set>
 7 <c:set property="age" target="${people2 }" value="16"></c:set>
 8 <h2>编号:${people2.id }</h2>
 9 <h2>姓名:${people2.name }</h2>
10 <h2>年龄:${people2.age }</h2>
11 </body>
View Code

 c:remove:

1 <body>
2 <c:set var="people" value="张三" scope="request"></c:set>
3 <h2><c:out value="${people}" default="没人啊"></c:out></h2>
4 <c:remove var="people" scope="request"/>
5 <h2><c:out value="${people}" default="没人啊"></c:out></h2>
6 </body>
View Code

 c:catch:

1 <body>
2 <c:catch var="errMsg">
3     <%
4         int a=1/0;
5     %>
6 </c:catch>
7 <h2>异常信息:${errMsg }</h2>
8 </body>
View Code

c:if:

 1 <body>
 2 <jsp:useBean id="people" class="com.java1234.model.People" scope="page"></jsp:useBean>
 3 <c:set property="id" target="${people }" value="007"></c:set>
 4 <c:set property="name" target="${people }" value="王二小"></c:set>
 5 <c:set property="age" target="${people }" value="16"></c:set>
 6 <c:if test="${people.name=='王二小' }" var="r" scope="page">
 7     <h2>是王二小</h2>
 8 </c:if>
 9 <c:if test="${people.age<18 }">
10     <h2>是未成年</h2>
11 </c:if>
12 </body>
View Code

c:choose/c:when/c:otherwise:  多条件判断:

 1 <body>
 2 <jsp:useBean id="people" class="com.java1234.model.People" scope="page"></jsp:useBean>
 3 <c:set property="id" target="${people }" value="007"></c:set>
 4 <c:set property="name" target="${people }" value="王二小"></c:set>
 5 <c:set property="age" target="${people }" value="19"></c:set>
 6 <c:choose>
 7     <c:when test="${people.age<18 }">
 8         <h2>小于18</h2>
 9     </c:when>
10     <c:when test="${people.age==18 }">
11         <h2>等于18</h2>
12     </c:when>
13     <c:otherwise>
14         <h2>大于18</h2>
15     </c:otherwise>
16 </c:choose>
17 </body>
View Code

c:forEach:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <%@ page import="com.java1234.model.*"%>
 3 <%@ page import="java.util.*"%>
 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12 <%
13     String dogs[]={"小黑","小黄","小白","小小"};
14     pageContext.setAttribute("dogs",dogs);
15 %>
16 <c:forEach var="dog" items="${dogs }">
17     ${dog }
18 </c:forEach>
19 <hr/>
20 <c:forEach var="dog" items="${dogs }" step="2">
21     ${dog }
22 </c:forEach>
23 <hr/>
24 <c:forEach var="dog" items="${dogs }" begin="1" end="2"><!--begin end是索引值-->
25     ${dog }
26 </c:forEach>
27 <hr/>
28 <%
29     List<People> pList=new ArrayList<People>();
30     pList.add(new People(1,"张三",10));
31     pList.add(new People(2,"李四",20));
32     pList.add(new People(3,"王五",30));
33     pageContext.setAttribute("pList",pList);
34 %>
35 <table>
36     <tr>
37         <th>编号</th>
38         <th>姓名</th>
39         <th>年龄</th>
40     </tr>
41     <c:forEach var="p" items="${pList }">
42         <tr>
43             <td>${p.id }</td>
44             <td>${p.name }</td>
45             <td>${p.age }</td>
46         </tr>
47     </c:forEach>
48 </table>
49 </body>
50 </html>
View Code

c:forTokens: 分隔输出:

 1 <body>
 2 <%
 3     String str1="www.java1234.com";
 4     String str2="张三,李四,王五";
 5     pageContext.setAttribute("str1",str1);
 6     pageContext.setAttribute("str2",str2);
 7 %>
 8 <c:forTokens items="${str1 }" delims="." var="s1">
 9     ${s1 }
10 </c:forTokens>
11 <hr/>
12 <c:forTokens items="${str2 }" delims="," var="s2">
13     ${s2 }
14 </c:forTokens>
15 </body>
View Code

c:import: 导入页面; 有点像jsp include:

1 <body>
2 <c:import url="c_forEach.jsp"></c:import>
3 <c:import url="c_if.jsp"></c:import>
4 </body>
View Code

c:url:

1 <body>
2 <c:url value="http://www.baidu.com" var="url">
3     <c:param name="name" value="xiaoming"></c:param>
4     <c:param name="age" value="26"></c:param>
5 </c:url>
6 <a href="${url }">百度</a>
7 </body>
View Code

c:redirect 客户端跳转:

 1 <body>
 2 <c:redirect url="target.jsp">
 3     <c:param name="name" value="xiaoming"></c:param>
 4     <c:param name="age" value="26"></c:param>
 5 </c:redirect>
 6 </body>
 7 
 8 target.jsp:
 9 <body>
10 <h2>姓名:${param.name }</h2>
11 <h2>年龄:${param.age }</h2>
12 </body>
View Code
原文地址:https://www.cnblogs.com/tenWood/p/6506320.html