JSTL 标准便签库 = ?
JSP 标准标签库(JSP Standard Tag Library,JSTL)是一个实现 Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断、数据管理格式化、XML 操作以及数据库访问。
在 developerWorks 上其新系列的第一篇文章中,软件工程师Mark Kolb 向您展示了如何使用 JSTL 标记来避免在 JSP 页面中使用脚本编制元素。您还将了解如何通过从表示层删除源代码来简化软件维护。
最后,您将了解 JSTL 经过简化的表达式语言,它允许在不必使用功能齐全的编程语言的情况下对 JSTL 操作指定动态属性值。
※使用JSTL前必须在本页面设置taglib指令(用于导入JSTL标签库)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
c:out 标签用来将作用域对象中数据输出到页面中
<%
request.setAttribute("weather", "今天天气怎么样?");
session.setAttribute("weather", "今天天气怎么样");
%>
<c:out value="${weather}" default="查询出错"></c:out>
<c:out value="${sessionScope.weather}" default="查询出错"></c:out>
※不指定作用域则默认从最小作用域开始取值
c:set标签用于将数据放入到作用域对象中,默认放入最小的作用域对象中。通过scope属性指定作用域
<c:set var="name" value="zhangsan" scope="request"></c:set>
${requestScope.name}
※c:set还可以标签中间添加html代码作为标签的value属性值
c:set插入表格标签
<c:set var="mytable">
<table border="1" style="border-collapse:collapse">
<tr><td>1</td><td>2</td></tr>
<tr><td>3</td><td>4</td></tr>
</table>
</c:set>
//通过jsp将表格以标签形式插入
${mytable}
c:remove将指定key从作用域中删除,通过scope属性指定作用域
<%
session.setAttribute("weather", "今天天气怎么样");
%>
<c:remove var="weather" scope="session"/>
${weather}
c:if标签做判断
<%
request.setAttribute("score", 50);
%>
<c:if test="${score >= 60}" var="合格">
成绩合格!
</c:if>
<c:if test="${!合格}">
成绩不合格!
</c:if>
c:choose/c:when/c:otherwise 组标签可以表示多条件判断
<%
request.setAttribute("score", 99);
%>
<c:choose>
<c:when test="${score >= 90 }">成绩优秀!</c:when>
<c:when test="${score >= 80 }">成绩良好!</c:when>
<c:when test="${score >= 60 }">成绩合格!</c:when>
<c:otherwise>成绩不合格</c:otherwise>
</c:choose>
※注意:
1.c:otherwise 不能出现在c:when 之上。
2.c:otherwise 和 c:when 之间、c:when 和 c:when 之间不能出现其他字符。
c:forEach 遍历数据,items 里填入一个可以遍历的结构
<c:forEach begin="1" end="10" step="2" var="num" varStatus="status">
<!-- step是判断条件-->
${num}---${status.first}---${status.last}---${status.index}---${status.count}<br>
</c:forEach>
----------------------------------- 分 割 线 --------------------------------------------------
//用户列表
<%
List list = new ArrayList();
list.add(new User("zhangsan","张三"));
list.add(new User("lisi","李四"));
list.add(new User("xiaoming","小明"));
list.add(new User("xiaohong","小红"));
request.setAttribute("persons", list);
%>
//创建用户表
<table border="1" style="border-collapse:collapse">
//先创建表头
<tr><td>用户名</td><td>真实姓名</td></tr>
//再通过forEach循环遍历list列表中的itmes属性
<c:forEach items="${persons}" var="user">
<tr><td>${user.username}</td><td>${user.realname}</td></tr>
</c:forEach>
</table>
c:import:将一个URL中的内容导入在本页面中一起显示
<c:import url="http://www.baidu.com"></c:import>
c:redirect:JSTL 重定向
<c:redirect url="http://localhost:8080/day04/testjstl.jsp"></c:redirect>
fmt:formatDate / fmt:parseDate 输出日期
<%
request.setAttribute("d1", new Date());
%>
//输出当前系统时间到页面
<fmt:formatDate value="${d1}" pattern="yyyy年MM月dd日 HH时mm分ss秒"/>
----------------------------------- 分 割 线 --------------------------------------------------
//将时间日期转换成指定格式后,输出到在页面上
<c:set var="nowdate" value="2019-07-19" />
<fmt:parseDate value="${nowdate}" var="key" pattern="yyyy-MM-dd"/>
<c:out value="${key}" />
fn :length 输出指定对象的字节长度
${fn :length(sessionScope.scope)}