10.1JSTL标签库的介绍
JSTL 标签库 全称是指 JSP Standard Tag Library JSP 标准标签库。是一个不断完善的开放源代码的 JSP 标签库。
EL 表达式主要是为了替换 jsp 中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个 jsp 页面变得更加简洁。
JSTL 由五个不同功能的标签库组成
功能范围 | URL | 前缀 |
---|---|---|
核心标签库(重点) | http://java.sun.com/jsp/jstl/core | c |
格式化 | http://java.sun.com/jsp/jstl/fmt | fmt |
函数 | http://java.sun.com/jsp/jstl/functions | fn |
数据库(不使用) | http://java.sun.com/jsp/jstl/sql | sql |
XML(不使用) | http://java.sun.com/jsp/jstl/xml | x |
在 jsp 标签库中使用 taglib 指令引入标签库:
Code标签库 | <%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %> |
XML标签库 | <%@ taglib prefix=“x” uri=“http://java.sun.com/jsp/jstl/xml” %> |
FML标签库 | <%@ taglib prefix=“fmt” uri=“http://java.sun.com/jsp/jstl/fmt” %> |
SQL标签库 | <%@ taglib prefix=“sql” uri=“http://java.sun.com/jsp/jstl/sql” %> |
FUNCTIONS 标签库 | <%@ taglib prefix=“fn” uri=“http://java.sun.com/jsp/jstl/functions” %> |
10.2JSTL标签库的使用
10.2.1JSTL 标签库的使用步骤
1、先导入 jstl 标签库的 jar 包:
taglibs-standard-impl-1.2.1.jar
taglibs-standard-spec-1.2.1.jar
2、 第二步,使用 taglib 指令引入标签库:
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
10.2.2core 核心库使用
10.2.2.1<c:set/>(很少使用)
set 标签可以往域中保存数据:
1. <%--
2. i.<c:set />
3. 作用:set 标签可以往域中保存数据
4. 域对象.setAttribute(key,value);
5. scope 属性设置保存到哪个域
6. page 表示 PageContext 域(默认值)
7. request 表示 Request 域
8. session 表示 Session 域
9. application 表示 ServletContext 域
10. var 属性设置 key 是多少
11. value 属性设置值
12. --%>
13. 保存之前:${ sessionScope.abc } <br>
14. <c:set scope="session" var="abc" value="abcValue"/>
15. 保存之后:${ sessionScope.abc } <br
10.2.2.2<c:if/>
if 标签用来做 if判断:
1. <%--
2. ii.<c:if />
3. if 标签用来做 if 判断。
4. test 属性表示判断的条件(使用 EL 表达式输出)
5. --%>
6. <c:if test="${ 12 == 12 }">
7. <h1>12 等于 12</h1>
8. </c:if>
9. <c:if test="${ 12 != 12 }">
10. <h1>12 不等于 12</h1>
11. </c:if>
10.2.2.3<c:choose><c:when><c:otherwise>
多路判断,作用跟 switch … case … default 非常接近
1. <%--
2. iii.<c:choose> <c:when> <c:otherwise>标签
3. 作用:多路判断。跟 switch ... case .... default 非常接近
4. choose 标签开始选择判断
5. when 标签表示每一种判断情况
6. test 属性表示当前这种判断情况的值
7. otherwise 标签表示剩下的情况
8. <c:choose> <c:when> <c:otherwise>标签使用时需要注意的点:
9. 1、标签里不能使用 html 注释,要使用 jsp 注释
10. 2、when 标签的父标签一定要是 choose 标签
11. --%>
12. <%
13. request.setAttribute("height", 180);
14. %>
15. <c:choose>
16. <%-- 这是 html 注释 --%>
17. <c:when test="${ requestScope.height > 190 }">
18. <h2>小巨人</h2>
19. </c:when>
20. <c:when test="${ requestScope.height > 180 }">
21. <h2>很高</h2>
22. </c:when>
23. <c:when test="${ requestScope.height > 170 }">
24. <h2>还可以</h2>
25. </c:when>
26. <c:otherwise>
27. <c:choose>
28. <c:when test="${requestScope.height > 160}">
29. <h3>大于 160</h3>
30. </c:when>
31. <c:when test="${requestScope.height > 150}">
32. <h3>大于 150</h3>
33. </c:when>
34. <c:when test="${requestScope.height > 140}">
35. <h3>大于 140</h3>
36. </c:when>
37. <c:otherwise>
38. 其他小于 140
39. </c:otherwise>
40. </c:choose>
41. </c:otherwise>
42. </c:choose>
10.2.2.4<c:forEach/>
作用是:遍历输出
1、 遍历输出1-10
1. <%--1.遍历 1 到 10,输出
2. begin 属性设置开始的索引
3. end 属性设置结束的索引
4. var 属性表示循环的变量(也是当前正在遍历到的数据)
5. for (int i = 1; i < 10; i++)
6. --%>
7. <table border="1">
8. <c:forEach begin="1" end="10" var="i">
9. <tr>
10. <td>第${i}行</td>
11. </tr>
12. </c:forEach>
13. </table>
2、 遍历Object数组
1. <%-- 2.遍历 Object 数组
2. for (Object item: arr)
3. items 表示遍历的数据源(遍历的集合)
4. var 表示当前遍历到的数据
5. --%>
6. <%
7. request.setAttribute("arr", new String[]{"18610541354","18688886666","18699998888"});
8. %>
9. <c:forEach items="${ requestScope.arr }" var="item">
10. ${ item } <br>
11. </c:forEach>
3、 遍历Map集合
1. <%
2. Map<String,Object> map = new HashMap<String, Object>();
3. map.put("key1", "value1");
4. map.put("key2", "value2");
5. map.put("key3", "value3");
6. // for ( Map.Entry<String,Object> entry : map.entrySet()) {
7. // }
8. request.setAttribute("map", map);
9. %>
10. <c:forEach items="${ requestScope.map }" var="entry">
11. <h1>${entry.key} = ${entry.value}</h1>
12. </c:forEach>
4、 遍历 List 集合—list 中存放 Student 类,有属性:编号,用户名,密码,年龄,电话信息
Student类:
1. public class Student {
2. //4.编号,用户名,密码,年龄,电话信息
3. private Integer id;
4. private String username;
5. private String password;
6. private Integer age;
7. private String phone
进行遍历:
1. <%--4.遍历 List 集合---list 中存放 Student 类,有属性:编号,用户名,密码,年龄,电话信息--%>
2. <%
3. List<Student> studentList = new ArrayList<Student>();
4. for (int i = 1; i <= 10; i++) {
5. studentList.add(new Student(i,"username"+i ,"pass"+i,18+i,"phone"+i));
6. }
7. request.setAttribute("stus", studentList);
8. %>
9. <table>
10. <tr>
11. <th>编号</th>
12. <th>用户名</th>
13. <th>密码</th>
14. <th>年龄</th>
15. <th>电话</th>
16. <th>操作</th>
17. </tr>
18. <%--
19. items 表示遍历的集合
20. var 表示遍历到的数据
21. begin 表示遍历的开始索引值
22. end 表示结束的索引值
23. step 属性表示遍历的步长值
24. varStatus 属性表示当前遍历到的数据的状态
25. for(int i = 1; i < 10; i+=2)
26. --%>
27. <c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu">
28. <tr>
29. <td>${stu.id}</td>
30. <td>${stu.username}</td>
31. <td>${stu.password}</td>
32. <td>${stu.age}</td>
33. <td>${stu.phone}</td>
34. <td>${status.step}</td>
35. </tr>
36. </c:forEach>
37. </table>