JSP 标准标签库(JSTL) 短命女 2022-04-01 02:42 217阅读 0赞 ## JSP 标准标签库(JSTL) ## JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。 JSTL支持通用的、结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签。 除了这些,它还提供了一个框架来使用集成JSTL的自定义标签。 根据JSTL标签所提供的功能,可以将其分为5个类别。 * **核心标签** * **格式化标签** * **SQL 标签** * **XML 标签** * **JSTL 函数** -------------------- ## JSTL 库安装 ## Apache Tomcat安装JSTL 库步骤如下: * 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip)。下载地址:[http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/][http_archive.apache.org_dist_jakarta_taglibs_standard_binaries] * 下载jakarta-taglibs-standard-1.1.1.zip 包并解压,将jakarta-taglibs-standard-1.1.1/lib/下的两个jar文件:standard.jar和jstl.jar文件拷贝到/WEB-INF/lib/下。 * 接下来我们在 web.xml 文件中添加以下配置: …… <jsp-config> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri> <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri> <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri> <taglib-location>/WEB-INF/c-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri> <taglib-location>/WEB-INF/sql-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x</taglib-uri> <taglib-location>/WEB-INF/x.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri> <taglib-location>/WEB-INF/x-rt.tld</taglib-location> </taglib> </jsp-config> …… 使用任何库,你必须在每个JSP文件中的头部包含<taglib>标签。 -------------------- ## 核心标签 ## 核心标签是最常用的JSTL标签。引用核心标签库的语法如下: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <table> <tbody> <tr> <th>标签</th> <th>描述</th> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-out-tag.html" rel="nofollow"><c:out></a></td> <td>用于在JSP中显示数据,就像<%= ... ></td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-set-tag.html" rel="nofollow"><c:set></a></td> <td>用于保存数据</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-remove-tag.html" rel="nofollow"><c:remove></a></td> <td>用于删除数据</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-catch-tag.html" rel="nofollow"><c:catch></a></td> <td>用来处理产生错误的异常状况,并且将错误信息储存起来</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-if-tag.html" rel="nofollow"><c:if></a></td> <td>与我们在一般程序中用的if一样</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-choose-tag.html" rel="nofollow"><c:choose></a></td> <td>本身只当做<c:when>和<c:otherwise>的父标签</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-choose-tag.html" rel="nofollow"><c:when></a></td> <td><c:choose>的子标签,用来判断条件是否成立</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-choose-tag.html" rel="nofollow"><c:otherwise></a></td> <td><c:choose>的子标签,接在<c:when>标签后,当<c:when>标签判断为false时被执行</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-import-tag.html" rel="nofollow"><c:import></a></td> <td>检索一个绝对或相对 URL,然后将其内容暴露给页面</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-foreach-tag.html" rel="nofollow"><c:forEach></a></td> <td>基础迭代标签,接受多种集合类型</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-foreach-tag.html" rel="nofollow"><c:forTokens></a></td> <td>根据指定的分隔符来分隔内容并迭代输出</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-param-tag.html" rel="nofollow"><c:param></a></td> <td>用来给包含或重定向的页面传递参数</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-redirect-tag.html" rel="nofollow"><c:redirect></a></td> <td>重定向至一个新的URL.</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-core-url-tag.html" rel="nofollow"><c:url></a></td> <td>使用可选的查询参数来创造一个URL</td> </tr> </tbody> </table> -------------------- ## 格式化标签 ## JSTL格式化标签用来格式化并输出文本、日期、时间、数字。引用格式化标签库的语法如下: <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <table> <tbody> <tr> <th>标签</th> <th>描述</th> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-formatnumber-tag.html" rel="nofollow"><fmt:formatNumber></a></td> <td>使用指定的格式或精度格式化数字</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-parsenumber-tag.html" rel="nofollow"><fmt:parseNumber></a></td> <td>解析一个代表着数字,货币或百分比的字符串</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-formatdate-tag.html" rel="nofollow"><fmt:formatDate></a></td> <td>使用指定的风格或模式格式化日期和时间</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-parsedate-tag.html" rel="nofollow"><fmt:parseDate></a></td> <td>解析一个代表着日期或时间的字符串</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-bundle-tag.html" rel="nofollow"><fmt:bundle></a></td> <td>绑定资源</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-setlocale-tag.html" rel="nofollow"><fmt:setLocale></a></td> <td>指定地区</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-setbundle-tag.html" rel="nofollow"><fmt:setBundle></a></td> <td>绑定资源</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-timezone-tag.html" rel="nofollow"><fmt:timeZone></a></td> <td>指定时区</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-settimezone-tag.html" rel="nofollow"><fmt:setTimeZone></a></td> <td>指定时区</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-message-tag.html" rel="nofollow"><fmt:message></a></td> <td>显示资源配置文件信息</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-format-requestencoding-tag.html" rel="nofollow"><fmt:requestEncoding></a></td> <td>设置request的字符编码</td> </tr> </tbody> </table> -------------------- ## SQL标签 ## JSTL SQL标签库提供了与关系型数据库(Oracle,MySQL,SQL Server等等)进行交互的标签。引用SQL标签库的语法如下: <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <table> <tbody> <tr> <th>标签</th> <th>描述</th> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-sql-setdatasource-tag.html" rel="nofollow"><sql:setDataSource></a></td> <td>指定数据源</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-sql-query-tag.html" rel="nofollow"><sql:query></a></td> <td>运行SQL查询语句</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-sql-update-tag.html" rel="nofollow"><sql:update></a></td> <td>运行SQL更新语句</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-sql-param-tag.html" rel="nofollow"><sql:param></a></td> <td>将SQL语句中的参数设为指定值</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-sql-dateparam-tag.html" rel="nofollow"><sql:dateParam></a></td> <td>将SQL语句中的日期参数设为指定的java.util.Date 对象值</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-sql-transaction-tag.html" rel="nofollow"><sql:transaction></a></td> <td>在共享数据库连接中提供嵌套的数据库行为元素,将所有语句以一个事务的形式来运行</td> </tr> </tbody> </table> -------------------- ## XML 标签 ## JSTL XML标签库提供了创建和操作XML文档的标签。引用XML标签库的语法如下: <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %> 在使用xml标签前,你必须将XML 和 XPath 的相关包拷贝至你的<Tomcat 安装目录>\\lib下: * <li**XercesImpl.jar:** 下载地址: [http://www.apache.org/dist/xerces/j/][http_www.apache.org_dist_xerces_j] * **xalan.jar:** 下载地址: [http://xml.apache.org/xalan-j/index.html][http_xml.apache.org_xalan-j_index.html] <table> <tbody> <tr> <th>标签</th> <th>描述</th> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-out-tag.html" rel="nofollow"><x:out></a></td> <td>与<%= ... >,类似,不过只用于XPath表达式</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-parse-tag.html" rel="nofollow"><x:parse></a></td> <td>解析 XML 数据</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-set-tag.html" rel="nofollow"><x:set></a></td> <td>设置XPath表达式</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-if-tag.html" rel="nofollow"><x:if></a></td> <td>判断XPath表达式,若为真,则执行本体中的内容,否则跳过本体</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-foreach-tag.html" rel="nofollow"><x:forEach></a></td> <td>迭代XML文档中的节点</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-choose-tag.html" rel="nofollow"><x:choose></a></td> <td><x:when>和<x:otherwise>的父标签</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-choose-tag.html" rel="nofollow"><x:when></a></td> <td><x:choose>的子标签,用来进行条件判断</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-choose-tag.html" rel="nofollow"><x:otherwise></a></td> <td><x:choose>的子标签,当<x:when>判断为false时被执行</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-transform-tag.html" rel="nofollow"><x:transform></a></td> <td>将XSL转换应用在XML文档中</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-xml-param-tag.html" rel="nofollow"><x:param></a></td> <td>与<x:transform>共同使用,用于设置XSL样式表</td> </tr> </tbody> </table> -------------------- ## JSTL函数 ## JSTL包含一系列标准函数,大部分是通用的字符串处理函数。引用JSTL函数库的语法如下: <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <table> <tbody> <tr> <th>函数</th> <th>描述</th> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-contains.html" rel="nofollow">fn:contains()</a></td> <td>测试输入的字符串是否包含指定的子串</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-containsignoreCase.html" rel="nofollow">fn:containsIgnoreCase()</a></td> <td>测试输入的字符串是否包含指定的子串,大小写不敏感</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-endswith.html" rel="nofollow">fn:endsWith()</a></td> <td>测试输入的字符串是否以指定的后缀结尾</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-escapexml.html" rel="nofollow">fn:escapeXml()</a></td> <td>跳过可以作为XML标记的字符</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-indexof.html" rel="nofollow">fn:indexOf()</a></td> <td>返回指定字符串在输入字符串中出现的位置</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-join.html" rel="nofollow">fn:join()</a></td> <td>将数组中的元素合成一个字符串然后输出</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-length.html" rel="nofollow">fn:length()</a></td> <td>返回字符串长度</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-replace.html" rel="nofollow">fn:replace()</a></td> <td>将输入字符串中指定的位置替换为指定的字符串然后返回</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-split.html" rel="nofollow">fn:split()</a></td> <td>将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-startswith.html" rel="nofollow">fn:startsWith()</a></td> <td>测试输入字符串是否以指定的前缀开始</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-substring.html" rel="nofollow">fn:substring()</a></td> <td>返回字符串的子集</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-substringafter.html" rel="nofollow">fn:substringAfter()</a></td> <td>返回字符串在指定子串之后的子集</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-substringbefore.html" rel="nofollow">fn:substringBefore()</a></td> <td>返回字符串在指定子串之前的子集</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-tolowercase.html" rel="nofollow">fn:toLowerCase()</a></td> <td>将字符串中的字符转为小写</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-touppercase.html" rel="nofollow">fn:toUpperCase()</a></td> <td>将字符串中的字符转为大写</td> </tr> <tr> <td><a href="https://www.w3cschool.cn/jsp/jstl-function-trim.html" rel="nofollow">fn:trim()</a></td> <td>移除首位的空白符</td> </tr> </tbody> </table> [http_archive.apache.org_dist_jakarta_taglibs_standard_binaries]: https://archive.apache.org/dist/jakarta/taglibs/standard/binaries/ [http_www.apache.org_dist_xerces_j]: https://www.apache.org/dist/xerces/j/ [http_xml.apache.org_xalan-j_index.html]: https://xml.apache.org/xalan-j/index.html
还没有评论,来说两句吧...