JSTL标签库
-
JSTL(JSP Standard Tag Library),JSP标准标签库
-
JSTL用于简化JSP开发,提高代码的可读性与可维护性
-
JSTL由SUN(Oracle)定义规范,由Apache Tomcat团队实现
下载JSTL标签库
官网地址:http://tomcat.apache.org/
JSTL v1.2.5组件介绍
作用域对象 | 描述 |
---|---|
taglibs-standard-spec-1.2.5.jar | 标签库定义包(必须) |
taglibs-standard-impl-1.2.5.jar | 标签库实现包(必须) |
taglibs-standard-jstlel-1.2.5.jar | EL表达式支持包(备选) |
taglibs-standard-compat-1.2.5.jar | 1.0版本兼容包(备选) |
进入官网首页找到Apache Standard Taglib 1.2.5 Released栏,download进入详细页面在最下方下载
安装JSTL标签库
JSTL有两种安装方式
- 将Jar文件复制到工程的/WEB-INF/lib目录(推荐)
- 将Jar文件复制到Tomcat安装目录的lib(全局组件依赖目录)目录(全局工程项目生效)
在Tomcat的lib目录中可以看到好多jar包文件:
servlet-api.jar servlet的实现
jsp-api.jar jsp的实现
el-api.jar el表达式的实现
还有一些tomcat运行相关的jar包,tomcat-i18n-es.jar国际化jar包等等
JSTL核心库
JSTL的标签库种类
JSTL按功能划分可划分为五类标签库
类别 |
---|
核心标签库 - core |
格式化输出标签库 - fmt |
SQL操作变迁库 - sql |
XML操作标签库 - xml |
函数标签库 - functions |
后3个现在基本不用了,因为在java中有了更好的方案来替代这种方式
引用JSTL核心库
- 核标签库(Core)是JSTL最重要的标签库,提供了JSTL的基础功能
- 在JSP页面顶端声明taglib指令引用,使用prefix="c"为核心标签库(core)声明一个前缀:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- JSTL核心标签库在taglibs-standard-impl.jar由META-INF/
c.tld
定义
小技巧:
选中ISO-8859-1按ctrl+F,replaceAll替换UTF-8
alt+/弹出智能提示,选择要引用的路径
JSTL判断标签
JSTL核心库提供了两组判断的标签
- <c:if> - 单分支标签
- <c:choose>、<c:when>、<c:otherwise> - 多分支判断
例子:
jstl.JstlServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("score", 99);
request.setAttribute("grade", "A");
request.getRequestDispatcher("/core.jsp").forward(request, response);
}
core.jsp
<%@ 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 charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>${requestScope.score }</h1>
<c:if test="${score>=60 }">
<h1 style="color:green">恭喜!</h1>
</c:if>
<c:if test="${score<60 }">
<h1 style="color:red">菜逼!</h1>
</c:if>
<!-- choose when otherwise -->
${grade }
<c:choose>
<c:when test="${grade=='A'}">
<h2>基本操作</h2>
</c:when>
<c:when test="${grade=='B'}">
<h2>有点菜</h2>
</c:when>
<c:when test="${grade=='C'}">
<h2>辣鸡</h2>
</c:when>
<c:when test="${grade=='D'}">
<h2>校霸</h2>
</c:when>
<c:otherwise>
<h2>大神</h2>
</c:otherwise>
</c:choose>
</body>
</html>
JSTL集合遍历
<c:forEach>标签用于遍历集合(Collection)中的每一个对象
<c:forEach var="p" items="${persons}" varStauts="idx">
第${idx.index+1}位<br/>
姓名:${p.name} 性别:${p.sex} 年龄:${p.age}
</c.forEach>
fmt(format)格式化标签库
- uri:
http://java.sun.com/jsp/jstl/fmt
- <fmt:formatDate value=“” pattern=“”> 格式化日期标签
- <fmt:formatNumber value=“” pattern=“”> 格式化数字标签
out标签
- out标签可以用于设置默认值,和让html按照原格式输出
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("amt", 19874454.154);
request.setAttribute("now", new java.util.Date());
request.setAttribute("html", "<a href='index.html'>index</a>");
request.setAttribute("nothing", null);
%>
<fmt:formatDate value="${requestScope.now }" pattern="yyyy年MM月dd日HH时mm分ss秒SSS毫秒"></fmt:formatDate>
<br/>
<p><fmt:formatNumber value="${amt }" pattern="0.00"></fmt:formatNumber></p>
<p>¥<fmt:formatNumber value="${amt }" pattern="0,000.00"></fmt:formatNumber>元</p>
<p>¥<fmt:formatNumber value="${amt }" pattern="0,00.00"></fmt:formatNumber>元</p>z
<!-- 设置默认值和是否转移xml -->
<h2>null默认值:<c:out value="${nothing }" default="无"></c:out></h2>
<h2><c:out value="${html }" escapeXml="true"></c:out></h2>
</body>
</html>