JSP标准标签库(JSTL) ╰+哭是因爲堅強的太久メ 2022-07-16 00:49 198阅读 0赞 JSP标准标签库(**JSTL**)是一个JSP标签集合,它封装了JSP应用的通用核心功能。 JSTL支持通用的、结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签。 除了这些,它还提供了一个框架来使用集成JSTL的自定义标签。 根据JSTL标签所提供的功能,可以将其分为5个类别。 * 核心标签 * 格式化标签 * SQL 标签 * XML 标签 * JSTL 函数 ## **JSTL库安装** ## Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip)。点击[官方下载地址][Link 1]下载。 下载jakarta-taglibs-standard-1.1.2.zip 包并解压,将jakarta-taglibs-standard-1.1.2/lib/下的两个jar文件:standard.jar和jstl.jar文件拷贝到/WEB-INF/lib/下。 **使用任何库,你必须在每个JSP文件中的头部包含`<taglib>`标签。** ## **核心标签** ## <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core_rt" %> ## **表达式控制** ## 这个是我们常用的 <table> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td><strong><code><c:out></code></strong></td> <td>用于在JSP中显示数据,就想<%=.. %>,区别在于 标签可以直接通过 . 操作符来访问属性</td> </tr> <tr> <td><code><c:set></code></td> <td>用于保存数据</td> </tr> <tr> <td><code><c:remove></code></td> <td>用于删除数据</td> </tr> <tr> <td><code><c:catch></code></td> <td>用来处理产生异常状况,并且将错误信息存储起来</td> </tr> </tbody> </table> ## **流程控制** ## <table> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td><code><c:if ></code></td> <td>与我们在一般程序中用的 if 一样,但是没有else操作,变相实现</td> </tr> <tr> <td><code><c:choose ></code></td> <td>本身只当做 <code><c:when></code> 和 <code><c:otherwise></code> 的父标签,相当于 switch 标签</td> </tr> <tr> <td><code><c:when></code></td> <td><code><c:choose></code> 的子标签,用来判断条件是否成立</td> </tr> <tr> <td><code><c:otherwise></code></td> <td><code><c:choose></code> 的子标签,接在 <code><c:when></code> 标签后,当 <code><c:when></code> 标签 判断为 false 时被执行</td> </tr> </tbody> </table> ## **循环(重点)** ## <table> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td><code><c:forEach></code></td> <td>基础迭代标签,接受多种集合类型, 默默的回忆,有哪些重点?</td> </tr> <tr> <td><code><c:forTokens></code></td> <td>根据指定的分隔符来分隔内容并迭代输出, 怎么说,真的很实用的功能!</td> </tr> </tbody> </table> ## **URL操作** ## <table> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td><code><c:import></code></td> <td>检索一个绝对或相对 URL,然后将其内容 暴露给页面 ,可以 替换 JSP的 动 态包含</td> </tr> <tr> <td><code><c:redirec t></code></td> <td>使用可选的查询参数来创造一个 URL</td> </tr> <tr> <td><code><c:url ></code></td> <td>重定向至一个新的 URL</td> </tr> <tr> <td><code><c:param ></code></td> <td>用来给包含或重定向的页面传递参数</td> </tr> <tr> <td><code><c:import></code></td> <td>想一想如果配合使用JSP的过滤器,那会是什么结果?请做一下实验!</td> </tr> </tbody> </table> ## **格式化标签库** ## 用于格式化并输出文本、日期、时间、数字,这里只介绍常用的两个标签,引用方 法: <%@taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %> ## **格式化数字** ## <table> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td><code><fmt:formatNumber></code></td> <td>使用指定的格式或精度格式化数字</td> </tr> </tbody> </table> **涉及到钱的时候,不要使用float/double,还有使用银行家四舍五入方式** ## **格式化日期** ## <table> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td><code><fmt:formatDate></code></td> <td>使用指定的风格或模式格式化日期和时间</td> </tr> </tbody> </table> **yyyy mm MM dd H h ss 都是含义,默背一下!** `<fmt:parseDate value="${personMaster.masterDate}"pattern="yyyy-MM-dd" var="masterDate"/>`—将字符串型日期转换为日期型 `<fmt:formatDate value="${masterDate}" pattern="yyyy-MM-dd" />`—对日期进行格式化 `<fmt:formatDate value="${masterDate}" pattern="yyyy-MM-dd" />`—直接对日期进行格式化 ## **函数标签库** ## 大部分都是通用的字符串处理函数,用于配合 EL 表达式使用。引用方法: <%@taglib prefix="fn" uri="http://java.sun.com/jstl/functions_rt" %> ## **JSTL函数** ## JSTL包含一系列标准函数,大部分是通用的字符串处理函数。引用JSTL函数库的语法如下: <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <table> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td>fn:contains()</td> <td>测试输入的字符串是否包含指定的子串,如: ${fn:contains(“ABC”, “a”)}</td> </tr> <tr> <td>fn:containsIgnoreCa se()</td> <td>测试输入的字符串是否包含指定的子串,大小写不敏感,如: ${fn:containsIgnoreCase(“ABC”, “a”)}</td> </tr> <tr> <td>fn:endsWith()</td> <td>测试输入的字符串是否以指定的后缀结尾,如: ${fn:endsWith(“ABC”, “bc”)}</td> </tr> <tr> <td>fn:startsWith()</td> <td>测试输入的字符串是否以指定的前缀开始,如: ${fn:startsWith(“ABC”, “a b”)}</td> </tr> <tr> <td>fn:escapeXm l()</td> <td>用于将所有特殊字符转化为字符实体码。它只包含一个 string 参数,返回一个 Str ing 类型的值</td> </tr> <tr> <td>fn:indexOf()</td> <td>用于取得子字符串与源字符串匹配的开始位置,若子字符串与源字符串中的内容 没有匹配成功将返回“ -1 ”。它包括 string 和 substring 两个参数,返回结果为 int 类型.如: ${fn:indexOf(“ABCD”,”aBC”)}</td> </tr> <tr> <td>fn:join()</td> <td>将数组中的元素合成一个字符串然后输出</td> </tr> <tr> <td>fn:length()</td> <td>返回字符串长度</td> </tr> <tr> <td>fn:replace()</td> <td>将输入字符串中指定的位置替换为指定的字符串然后返回</td> </tr> <tr> <td>fn:split()</td> <td>将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回</td> </tr> <tr> <td>fn:substring()</td> <td>返回字符串的子集,如: ${fn:substring(“ABC”,”1”,”2”)}</td> </tr> <tr> <td>fn:substringA fter()</td> <td>返回字符串在指定子串之后的子集,如: ${fn:substringAfter(“ABCD”,”B C”)}</td> </tr> <tr> <td>fn:substringB efore()</td> <td>返回字符串在指定子串之前的子集,如: ${fn:substringBefore(“AB C”,”1”,”2”)}</td> </tr> <tr> <td>fn:toLowerCase()</td> <td>将字符串中的字符转为小写</td> </tr> <tr> <td>fn:toUpperCase()</td> <td>将字符串中的字符转为大写</td> </tr> <tr> <td>fn:trim()</td> <td>移除首位的空白符</td> </tr> </tbody> </table> ## **SQL标签** ## JSTL SQL标签库提供了与关系型数据库(Oracle,MySQL,SQL Server等等)进行交互的标签。引用SQL标签库的语法如下: <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <table> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td><code><sql:setDataSource></code></td> <td>指定数据源</td> </tr> <tr> <td><code><sql:query></code></td> <td>运行SQL查询语句</td> </tr> <tr> <td><code><sql:update></code></td> <td>运行SQL更新语句</td> </tr> <tr> <td><code><sql:param></code></td> <td>将SQL语句中的参数设为指定值</td> </tr> <tr> <td><code><sql:dateParam></code></td> <td>将SQL语句中的日期参数设为指定的java.util.Date 对象值</td> </tr> <tr> <td><code><sql:transaction></code></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`下: 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> <thead> <tr> <th>标签</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td><code><x:out></code></td> <td>与<%= … >,类似,不过只用于XPath表达式</td> </tr> <tr> <td><code><x:parse></code></td> <td>解析 XML 数据</td> </tr> <tr> <td><code><x:set></code></td> <td>设置XPath表达式</td> </tr> <tr> <td><code><x:if></code></td> <td>判断XPath表达式,若为真,则执行本体中的内容,否则跳过本体</td> </tr> <tr> <td><code><x:forEach></code></td> <td>迭代XML文档中的节点</td> </tr> <tr> <td><code><x:choose></code></td> <td><code><x:when></code>和<code><x:otherwise></code>的父标签</td> </tr> <tr> <td><code><x:when></code></td> <td><code><x:choose></code>的子标签,用来进行条件判断</td> </tr> <tr> <td><code><x:otherwise></code></td> <td><code><x:choose></code>的子标签,当<code><x:when></code>判断为false时被执行</td> </tr> <tr> <td><code><x:transform></code></td> <td>将XSL转换应用在XML文档中</td> </tr> <tr> <td><code><x:param></code></td> <td>与<code><x:transform></code>共同使用,用于设置XSL样式表</td> </tr> </tbody> </table> [Link 1]: http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/ [http_www.apache.org_dist_xerces_j]: http://www.apache.org/dist/xerces/j/ [http_xml.apache.org_xalan-j_index.html]: http://xml.apache.org/xalan-j/index.html
还没有评论,来说两句吧...