java Web JSTL介绍及基本应用
STL虽然被称为标准标签库,而实际上这个标签库是由5个不同功能的标签库共同组成的。在JSTL1.1规范中,为这5个标签库分别指定了不同的URI以及建议使用的前缀,如下所示:
标签库 | 标签库的URI | 前缀 |
---|---|---|
Core | http://java.sun.com/jsp/jstl/core | c |
I18N | http://java.sun.com/jsp/jstl/fmt | fmt |
SQL | http://java.sun.com/jsp/jstl/sql | sql |
XML | http://java.sun.com/jsp/jstl/xml | x |
Functions | http://java.sun.com/jsp/jstl/functions |
基本应用
需要先引入jstl.jar 和 standard.jar两个jar包
其次要在页面上引入标签库:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head></head> <body> <c:out value="Hello World!"></c:out> </body> </html>
基本应用(以循环和格式化日期为例):
foreach遍历数组和集合:
<%@page import="java.util.Map"%> <%@page import="java.util.HashMap"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.Collections"%> <%@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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 需求: 遍历域容器中的复杂数据(数组, list, map) --> <% int[] arr = {111, 333, 555}; pageContext.setAttribute("arr1", arr); %> <c:forEach items="${arr1 }" var="e" varStatus="vs"> ${vs.index } ${vs.count } ${e } <br/> </c:forEach> <hr/> <% List list = new ArrayList(); Collections.addAll(list, "aaa", "ccc", "eee"); pageContext.setAttribute("list1", list); %> <c:forEach items="${list1 }" var="e"> ${e }<br/> </c:forEach> <hr/> <% Map<String, Integer> map = new HashMap<String, Integer>(); map.put("aaa", 111); map.put("ccc", 333); map.put("eee.ddd.bbb", 555); map.put("fff", 666); pageContext.setAttribute("map1", map); %> <c:forEach items="${map1 }" var="en"> ${en.key } === ${en.value } <br/> </c:forEach> </body> </html>
for循环遍历数组和集合:
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h3>使用forEach输出 1~10, 奇数为红色,偶数为蓝色</h3> <c:forEach begin="1" end="10" step="1" var="i"> <font color="${i%2!=0?'red':'blue' }">${i }</font> </c:forEach> <br/> <c:forEach begin="1" end="10" step="4" var="i" varStatus="vs"> <font color="${vs.count%2!=0?'red':'blue' }">${i }</font> </c:forEach> <h3>使用forEach完成 1~10的和</h3> <h4>java</h4> <% int sum = 0; for(int i=1;i<=10;i++) { sum += i; } out.print("java 和:" + sum); %> <h4>jstl + el</h4> <c:set var="sum" value="0"></c:set> <c:forEach begin="1" end="10" step="1" var="i"> <c:set var="sum" value="${sum + i }"></c:set> </c:forEach> EL 和: ${sum } </body> </html>
格式化日期:
<%@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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 格式化日期 --> <% Date date = new Date(); pageContext.setAttribute("date1", date); %> <fmt:formatDate value="${date1 }" pattern="yyyy-MM-dd HH:mm:ss"/> </body> </html>
其他应用请自行百度。