JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。
JSTL 库安装
Apache Tomcat安装JSTL 库步骤如下:
从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip)。
官方下载地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
下载jakarta-taglibs-standard-1.1.2.zip 包并解压,将jakarta-taglibs-standard-1.1.2/lib/下的两个jar文件:standard.jar和jstl.jar文件拷贝到/WEB-INF/lib/下。
核心标签是最常用的JSTL标签。引用核心标签库的语法如下:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- 1
<c:out> 用于在JSP中显示数据,就像<%= ... >
<c:set> 用于保存数据
<c:remove> 用于删除数据
<c:catch> 用来处理产生错误的异常状况,并且将错误信息储存起来
<c:if> 与我们在一般程序中用的if一样
<c:choose> 本身只当做<c:when>和<c:otherwise>的父标签
<c:when> <c:choose>的子标签,用来判断条件是否成立
<c:otherwise> <c:choose>的子标签,接在<c:when>标签后,当<c:when>标签判断为false时被执行
<c:import> 检索一个绝对或相对 URL,然后将其内容暴露给页面
<c:forEach> 基础迭代标签,接受多种集合类型
<c:forTokens> 根据指定的分隔符来分隔内容并迭代输出
<c:param> 用来给包含或重定向的页面传递参数
<c:redirect> 重定向至一个新的URL.
<c:url> 使用可选的查询参数来创造一个URL
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
Customer
package com.safly;
public class Customer {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Customer() {
super();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
我们来看看c:out、c:set的简单用法吧:
c:out语法格式
<c:out value="<string>" default="<string>" escapeXml="<true|false>"/>
- 1
属性 描述 是否必要 默认值
value 要输出的内容 是 无
default 输出的默认值 否 主体中的内容
escapeXml 是否忽略XML特殊字符 否 true
- 1
- 2
- 3
- 4
c:set语法格式
<c:set
var="<string>"
value="<string>"
target="<string>"
property="<string>"
scope="<string>"/>
- 1
- 2
- 3
- 4
- 5
- 6
属性 描述 是否必要 默认值
value 要存储的值 否 主体的内容
target 要修改的属性所属的对象 否 无
property 要修改的属性 否 无
var 存储信息的变量 否 无
scope var属性的作用域 否 Page
- 1
- 2
- 3
- 4
- 5
- 6
实例代码如下:
<%
request.setAttribute("book", "java");
%>
book:
<c:out value="${requestScope.book}"></c:out>
<c:set var="name" value="safly" scope="page"></c:set>
<!-- 等价于下面一行代码,为域对象赋值 -->
<%
pageContext.setAttribute("name", "safly");
%>
name:${pageScope.name }
<%
Customer cust = new Customer();
cust.setId(101);
request.setAttribute("cust", cust);
%>
ID:${requestScope.cust.id }
<c:set target="${requestScope.cust}" property="id" value="${param.id}"></c:set>
ID:${requestScope.cust.id }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
浏览器输入http://localhost:8080/day01/mytag.jsp?id=234
页面输出结果如下
book: java name:safly ID:101 ID:234
- 1
我们来看看c:if、c:choose标签的简单用法吧:
c:choose语法格式
<c:choose>
<c:when test="<boolean>"/>
...
</c:when>
<c:when test="<boolean>"/>
...
</c:when>
...
...
<c:otherwise>
...
</c:otherwise>
</c:choose>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
<c:choose>标签没有属性。
<c:when>标签只有一个属性,在下表中有给出。
<c:otherwise>标签没有属性。
- 1
- 2
- 3
c:if语法格式
<c:if test="<boolean>" var="<string>" scope="<string>">
...
</c:if>
- 1
- 2
- 3
属性 描述 是否必要 默认值
test 条件 是 无
var 用于存储条件结果的变量 否 无
scope var属性的作用域 否 page
- 1
- 2
- 3
- 4
实例代码如下:
<c:set value="20" var="age" scope="request"></c:set>
<c:if test="${requestScope.age > 18}">成年了</c:if>
<c:if test="${param.age > 18}" var="isAdult" scope="request"></c:if>
isAdult:<c:out value="${requestScope.isAdult}"></c:out><br>
<c:choose>
<c:when test="${param.age > 60}">老年</c:when>
<c:when test="${param.age > 40}">中年</c:when>
<c:otherwise>青年</c:otherwise>
</c:choose>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
浏览器输入
http://localhost:8080/day01/mytag.jsp?age=45
输出结果如下:
成年了 isAdult:true
中年
- 1
- 2
我们来看看c:forEach标签的简单用法吧:
c:forEach语法格式
<c:forEach
items="<object>"
begin="<int>"
end="<int>"
step="<int>"
var="<string>"
varStatus="<string>">
...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
属性 描述 是否必要 默认值
items 要被循环的信息 否 无
begin 开始的元素(0=第一个元素,1=第二个元素) 否 0
end 最后一个元素(0=第一个元素,1=第二个元素) 否 Last element
step 每一次迭代的步长 否 1
var 代表当前条目的变量名称 否 无
varStatus 代表循环状态的变量名称 否 无
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
<%
List<Customer> custs = new ArrayList<Customer>();
custs.add(new Customer(1,"AA"));
custs.add(new Customer(2,"BB"));
custs.add(new Customer(3,"CC"));
custs.add(new Customer(4,"DD"));
custs.add(new Customer(5,"EE"));
custs.add(new Customer(6,"FF"));
request.setAttribute("custs",custs);
%>
<c:forEach items="${requestScope.custs}" var="safly" begin="1" step="2" end="5">
${safly.id }:${safly.name }
</c:forEach>
<br>
<%
Map<String,Customer> custMap = new HashMap<String,Customer>();
custMap.put("a",new Customer(1,"AA"));
custMap.put("b",new Customer(2,"BB"));
custMap.put("c",new Customer(3,"CC"));
custMap.put("d",new Customer(4,"DD"));
custMap.put("e",new Customer(5,"EE"));
request.setAttribute("custMap",custMap);
%>
<c:forEach items="${requestScope.custMap}" var="safly">
${safly.key }---${safly.value.id }--${safly.value.name }<br>
</c:forEach>
<%
String [] names = new String[]{"A","B","C"};
request.setAttribute("names",names);
%>
<c:forEach var="safly" items="${names}">
${safly }
</c:forEach>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
http://localhost:8080/day01/mytag.jsp、
输出结果如下:
2:BB 4:DD 6:FF
d---4--DD
e---5--EE
b---2--BB
c---3--CC
a---1--AA
A B C