一、简介
Java Server Pages Standard Tag Libray(JSTL):JSP 标准标签库
核心标签库:http://java.sun.com/jsp/jstl/core 包含 Web 应用的常见工作,
比如:循环、表达式赋值、基本输入输出等。
格式化标签库:http://java.sun.com/jsp/jstl/fmt 用来格式化显示数据的工作,
比如:对不同区域的日期格式化等。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jstl的使用</title>
</head>
<body>
<%--
1、导入jstl标签库所需要的jar包 (jstl.jar、standard.jar)
2、在jsp页面引入指定的库
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3、使用标签库
<c:标签名 属性名="属性值" ></c:标签名>
--%>
<%
request.setAttribute("num", 1);
%>
<c:if test="${num > 1 }">
<h2>Hello JSTL!</h2>
</c:if>
<%
request.setAttribute("score", 50);
// 获取域对象中的值
Integer score = (Integer)request.getAttribute("score");
// 条件判断
if (score < 60) {
%>
<h2>你不及格哦!</h2>
<%
} else if (score >= 60 && score < 80) {
%>
<h2>还可以!</h2>
<%
}
%>
</body>
</html>
二、标签的使用
1.条件动作标签
条件动作指令用于处理页面的输出结果依赖于某些输入值的情况,在 Java 中是利用 if、 if…else 和 switch 语句来进行处理的。在 JSTL 中也有 4 个标签可以执行条件式动作指令:if、 choose、when 和 otherwise。
1)if标签
if 标签先对某个条件进行测试,如果该条件运算结果为 true, 则处理它的主体内容.
测试结果保存在一个 Boolean 对象中,并创建一个限域变量来引用 Boolean 对象。
可以利用 var 属性设置限域变量名,利用 scope 属性来指定其作用范围。
if 的语法有两种形式:
没有主体内容
<c:if test=“返回boolean类型的条件” var=“设置限域变量名的,用来接收返回值的” scope=“限域对象的范围”></c:if>
有主体内容
<c:if test=“返回boolean类型的条件” var=“设置限域变量名的,用来接收返回值的” scope=“限域对象的范围”>
要执行的代码
</c:if>
常用属性:
test:返回boolean类型的条件,一般操作的都是域对象中的值
var:用来定义接收返回值的变量,但是该变量是存在域对象中的
scope:var设置的变量名所存放的域范围page\request\session\application
注:在jstl中没有else标签,如果出现需要else的情况,可以使用两个if,设置相反的;两个条件来实现
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>if标签</title>
</head>
<body>
<%
request.setAttribute("num", 0);
request.setAttribute("score", 50);
%>
<c:if test="${num > 0 }" var="flag" scope="page"></c:if> ${pageScope.flag }
<br>
<c:if test="${num > 0 }">
<h2>num比0大</h2>
</c:if>
<c:if test="${num <= 0 }">
<h2>num比0小</h2>
</c:if>
<br><hr><br>
<c:if test="${score < 60 }">
<h2>不及格!</h2>
</c:if>
<c:if test="${score >= 60 && score < 80}">
<h2>继续努力哦!</h2>
</c:if>
<c:if test="${score >= 80 && score < 100}">
<h2>哎哟不错哦!</h2>
</c:if>
<c:if test="${score == 100}">
<h2>学霸!</h2>
</c:if>
</body>
</html>
2)chose、when和otherwise标签
1、choose标签中至少包含一个when标签,可以没有otherwise标签 (Illegal “choose” without child “when” tag)
2、choose标签中只能有when标签和otherwise标签(Illegal child tag in “c:choose” tag: “c:if” tag)
3、when标签和otherwise标签可以包含其他标签
4、choose标签和otherwise标签没有属性,when标签必须有test属性
5、otherwise标签必须放在最后一个when标签之后 (Illegal “c:when” after “c:otherwise” tag in “c:choose” tag.)
6、当所有的when标签的条件不成立时,才会执行otherwise标签
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>choose、when 和 otherwise 标签</title>
</head>
<body>
<%
request.setAttribute("score", 50);
%>
<c:choose>
<c:when test="${score < 60 }">
<h2>你不及格!</h2>
</c:when>
<c:when test="${score >= 60 && score < 80 }">
<h2>继续努力!</h2>
</c:when>
<c:when test="${score >= 80 && score < 100 }">
<h2>哎哟不错哦!</h2>
</c:when>
<c:otherwise>
<h2>你好强!</h2>
</c:otherwise>
</c:choose>
</body>
</html>
2.迭代标签
forEach 是将一个主体内容迭代多次,或者迭代一个对象集合。
1、遍历主体内容多次
<c:forEach begin=“开始的数” end=“结束的数” var=“接收当前被循环的成员” step=“循环的间隔数”></c:forEach>
2、遍历集合、map等
<c:forEach items=“被循环的对象” var=“接收当前被循环的成员” varStatus=“当前成员的相关信息”></c:forEach>
常用属性:
items:需要被遍历的对象(可以是集合、map等,都是存放在域对象中的数据)
var:限域变量名,用来每次被遍历到的成员
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>forEach标签</title>
</head>
<body>
<c:forEach begin="1" end="10" var="i" step="3">
${i }
</c:forEach>
<hr/>
<%
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("mylist", list);
Map map = new HashMap();
map.put("aaa", "111");
map.put("bbb", 2222);
map.put("ccc-a", 333);
request.setAttribute("mymap", map);
%>
<c:forEach items="${mylist }" var="item" varStatus="p">
${item } - 当前下标:${p.index } - 当前次数:${p.count } - 是否是第一次被循环:${p.first } - 是否是最后一次被循环:${p.last } <br>
</c:forEach>
<br>
<c:forEach items="${mymap }" var="item">
${item } - ${item.key } - ${item.value } <br>
</c:forEach>
</body>
</html>
我们在这来一个例题
九九乘法表
普通for循环和foreach两种代码对比
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" style="border-collapse: collapse;">
<%
for (int i = 1; i <= 9; i++) {
%>
<tr>
<%
for (int j = 1; j <= i; j++) {
%>
<td>
<%=j %> * <%=i %> = <%=(j*i) %>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
<hr>
<table border="1" style="border-collapse: collapse;">
<c:forEach begin="1" end="9" var="i">
<tr>
<c:forEach begin="1" end="${i }" var="j">
<td>${j } * ${i } = ${j*i }</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
3.格式化动作指令
- formatNumber 标签
formatNumber 标签 该标签用指定的格式或精度来格式化数字.
fmt:formatNumber标签有如下属性:
使用该标签时,有两种语法:
无主体内容
<fmt:formatNumber value=“要被格式化的数值” type=“被格式化的类型”/>
常用属性:
value:要被格式化的数值
type:被格式化的类型
number数值型
percent百分比类型
currency货币类型
var:限域变量名,用来接收格式化好的返回值;如果设置var属性,则值不会再输出,需要使用el表达式输出var设置的值
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<fmt:formatNumber value="100" type="percent"/> <br>
<fmt:formatNumber value="100" type="currency"/> <br>
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="100" type="currency"/> <br>
<fmt:formatNumber value="100" type="number" var="num"/> ${num } <br>
<hr>
<fmt:formatNumber type="percent" >
10
</fmt:formatNumber>
<br>
<fmt:formatNumber type="currency" var="num2" >
10
</fmt:formatNumber>
${num2 }
</body>
</html>
2) formatDate 标签
使用指定的风格或模式格式化日期和时间,<fmt:formatDate>标签有如下属性:
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>formatDate 标签</title>
</head>
<body>
<%
request.setAttribute("mydate", new Date());
%>
${mydate } <br>
<fmt:formatDate value="${mydate }"/><br>
<fmt:formatDate value="${mydate }" type="date"/><br>
<fmt:formatDate value="${mydate }" type="time"/><br>
<fmt:formatDate value="${mydate }" type="both"/><br>
<fmt:formatDate value="${mydate }" type="both" dateStyle="long" timeStyle="full"/><br>
<fmt:formatDate value="${mydate }" pattern="yyyy/MM/dd HH:mm:ss"/><br>
<fmt:formatDate value="${mydate }" pattern="HH:mm:ss yyyy/MM/dd" var="date1"/>${date1 }<br>
</body>
</html>
3) parseNumber 标签
利用 parseNumber 标签可以将数字、货币或百分比的字符串表示法解析成
指定语言环 境的数字。即解析一个代表着数字,货币或百分比的字符串。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>parseDate标签</title>
</head>
<body>
<%
request.setAttribute("date1", "2019-5-6 17:33:58");
request.setAttribute("date2", "21-05-2017");
request.setAttribute("date3", "2019-5-6");
%>
${date1 }<br>
${date2 }<br>
<fmt:parseDate value="${date3 }"></fmt:parseDate><br>
<fmt:parseDate value="${date1 }" type="both"></fmt:parseDate><br>
<fmt:parseDate value="${date2 }" pattern="dd-MM-yyyy"></fmt:parseDate><br>
<fmt:parseDate value="${date3 }" pattern="yyyy-MM-dd"></fmt:parseDate><br>
</body>
</html>
- parseDate 标签
此标签为指定区域解析日期和时间的字符串表示法。即解析一个代表着日期
或时间的字 符串。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>parseNumber标签</title>
</head>
<body>
<fmt:parseNumber value="¥100.00" type="currency"></fmt:parseNumber> <br>
<fmt:parseNumber value="100%" type="percent"></fmt:parseNumber> <br>
</body>
</html>