小白教程
所有教程
关于
Search
172.70.131.190
172.70.131.190
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“JSP 标准标签库(JSTL)”的源代码
本页内容
上一节:
JSP_发送邮件
下一节:
JSP_连接数据库
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:JSP 标准标签库(JSTL)}}[[Category:JSP 教程|24]] = 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://static.xiaobai.wang/download/jakarta-taglibs-standard-1.1.2.tar.gz jakarta-taglibs-standard-1.1.2.zip] 下载 '''jakarta-taglibs-standard-1.1.2.zip''' 包并解压,将 '''jakarta-taglibs-standard-1.1.2/lib/''' 下的两个 jar 文件:'''standard.jar''' 和 '''jstl.jar''' 文件拷贝到 '''/WEB-INF/lib/''' 下。 将 tld 下的需要引入的 tld 文件复制到 WEB-INF 目录下。 接下来我们在 web.xml 文件中添加以下配置: <sample title="" desc="" lang="jsp" hererun="1"> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <jsp-config> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/fmt-rt</taglib-uri> <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri> <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core-rt</taglib-uri> <taglib-location>/WEB-INF/c-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/sql-rt</taglib-uri> <taglib-location>/WEB-INF/sql-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/x</taglib-uri> <taglib-location>/WEB-INF/x.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/x-rt</taglib-uri> <taglib-location>/WEB-INF/x-rt.tld</taglib-location> </taglib> </jsp-config> </web-app> </sample> 使用任何库,你必须在每个 JSP 文件中的头部包含 <taglib> 标签。 == 核心标签 == 核心标签是最常用的 JSTL标签。引用核心标签库的语法如下: <sample title="" desc="" lang="jsp" hererun="1"> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> </sample> {| class="table table-striped table-hover" ! 标签 ! 描述 |- | [[jstl core out tag|<c:out>]] | 用于在JSP中显示数据,就像<%= ... > |- | [[jstl core set tag|<c:set>]] | 用于保存数据 |- | [[jstl core remove tag|<c:remove>]] | 用于删除数据 |- | [[jstl core catch tag|<c:catch>]] | 用来处理产生错误的异常状况,并且将错误信息储存起来 |- | [[jstl core if tag|<c:if>]] | 与我们在一般程序中用的if一样 |- | [[jstl core choose tag|<c:choose>]] | 本身只当做<c:when>和<c:otherwise>的父标签 |- | [[jstl core choose tag|<c:when>]] | <c:choose>的子标签,用来判断条件是否成立 |- | [[jstl core choose tag|<c:otherwise>]] | <c:choose>的子标签,接在<c:when>标签后,当<c:when>标签判断为false时被执行 |- | [[jstl core import tag|<c:import>]] | 检索一个绝对或相对 URL,然后将其内容暴露给页面 |- | [[jstl core foreach tag|<c:forEach>]] | 基础迭代标签,接受多种集合类型 |- | [[jstl core foreach tag|<c:forTokens>]] | 根据指定的分隔符来分隔内容并迭代输出 |- | [[jstl core param tag|<c:param>]] | 用来给包含或重定向的页面传递参数 |- | [[jstl core redirect tag|<c:redirect>]] | 重定向至一个新的URL. |- | [[jstl core url tag|<c:url>]] | 使用可选的查询参数来创造一个URL |} == 格式化标签 == JSTL格式化标签用来格式化并输出文本、日期、时间、数字。引用格式化标签库的语法如下: <sample title="" desc="" lang="jsp" hererun="1"> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> </sample> {| class="table table-striped table-hover" ! 标签 ! 描述 |- | [[jstl format formatnumber tag|<fmt:formatNumber>]] | 使用指定的格式或精度格式化数字 |- | [[jstl format parsenumber tag|<fmt:parseNumber>]] | 解析一个代表着数字,货币或百分比的字符串 |- | [[jstl format formatdate tag|<fmt:formatDate>]] | 使用指定的风格或模式格式化日期和时间 |- | [[jstl format parsedate tag|<fmt:parseDate>]] | 解析一个代表着日期或时间的字符串 |- | [[jstl format bundle tag|<fmt:bundle>]] | 绑定资源 |- | [[jstl format setlocale tag|<fmt:setLocale>]] | 指定地区 |- | [[jstl format setbundle tag|<fmt:setBundle>]] | 绑定资源 |- | [[jstl format timezone tag|<fmt:timeZone>]] | 指定时区 |- | [[jstl format settimezone tag|<fmt:setTimeZone>]] | 指定时区 |- | [[jstl format message tag|<fmt:message>]] | 显示资源配置文件信息 |- | [[jstl format requestencoding tag|<fmt:requestEncoding>]] | 设置request的字符编码 |} == SQL标签 == JSTL SQL标签库提供了与关系型数据库(Oracle,MySQL,SQL Server等等)进行交互的标签。引用SQL标签库的语法如下: <sample title="" desc="" lang="jsp" hererun="1"> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> </sample> {| class="table table-striped table-hover" ! 标签 ! 描述 |- | [[jstl sql setdatasource tag|<sql:setDataSource>]] | 指定数据源 |- | [[jstl sql query tag|<sql:query>]] | 运行SQL查询语句 |- | [[jstl sql update tag|<sql:update>]] | 运行SQL更新语句 |- | [[jstl sql param tag|<sql:param>]] | 将SQL语句中的参数设为指定值 |- | [[jstl sql dateparam tag|<sql:dateParam>]] | 将SQL语句中的日期参数设为指定的java.util.Date 对象值 |- | [[jstl sql transaction tag|<sql:transaction>]] | 在共享数据库连接中提供嵌套的数据库行为元素,将所有语句以一个事务的形式来运行 |} == XML 标签 == JSTL XML标签库提供了创建和操作XML文档的标签。引用XML标签库的语法如下: <sample title="" desc="" lang="jsp" hererun="1"> <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %> </sample> 在使用xml标签前,你必须将XML 和 XPath 的相关包拷贝至你的<Tomcat 安装目录>\lib下: <p>'''XercesImpl.jar'''</p> <p>下载地址: http://www.apache.org/dist/xerces/j/</p> <p>'''xalan.jar'''</p> <p>下载地址: http://xml.apache.org/xalan-j/index.html</p> {| class="table table-striped table-hover" ! 标签 ! 描述 |- | [[jstl xml out tag|<x:out>]] | 与<%= ... >,类似,不过只用于XPath表达式 |- | [[jstl xml parse tag|<x:parse>]] | 解析 XML 数据 |- | [[jstl xml set tag|<x:set>]] | 设置XPath表达式 |- | [[jstl xml if tag|<x:if>]] | 判断XPath表达式,若为真,则执行本体中的内容,否则跳过本体 |- | [[jstl xml foreach tag|<x:forEach>]] | 迭代XML文档中的节点 |- | [[jstl xml choose tag|<x:choose>]] | <x:when>和<x:otherwise>的父标签 |- | [[jstl xml choose tag|<x:when>]] | <x:choose>的子标签,用来进行条件判断 |- | [[jstl xml choose tag|<x:otherwise>]] | <x:choose>的子标签,当<x:when>判断为false时被执行 |- | [[jstl xml transform tag|<x:transform>]] | 将XSL转换应用在XML文档中 |- | [[jstl xml param tag|<x:param>]] | 与<x:transform>共同使用,用于设置XSL样式表 |} == JSTL函数 == JSTL包含一系列标准函数,大部分是通用的字符串处理函数。引用JSTL函数库的语法如下: <sample title="" desc="" lang="jsp" hererun="1"> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> </sample> {| class="table table-striped table-hover" ! 函数 ! 描述 |- | [[jstl function contains|fn:contains()]] | 测试输入的字符串是否包含指定的子串 |- | [[jstl function containsignoreCase|fn:containsIgnoreCase()]] | 测试输入的字符串是否包含指定的子串,大小写不敏感 |- | [[jstl function endswith|fn:endsWith()]] | 测试输入的字符串是否以指定的后缀结尾 |- | [[jstl function escapexml|fn:escapeXml()]] | 跳过可以作为XML标记的字符 |- | [[jstl function indexof|fn:indexOf()]] | 返回指定字符串在输入字符串中出现的位置 |- | [[jstl function join|fn:join()]] | 将数组中的元素合成一个字符串然后输出 |- | [[jstl function length|fn:length()]] | 返回字符串长度 |- | [[jstl function replace|fn:replace()]] | 将输入字符串中指定的位置替换为指定的字符串然后返回 |- | [[jstl function split|fn:split()]] | 将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回 |- | [[jstl function startswith|fn:startsWith()]] | 测试输入字符串是否以指定的前缀开始 |- | [[jstl function substring|fn:substring()]] | 返回字符串的子集 |- | [[jstl function substringafter|fn:substringAfter()]] | 返回字符串在指定子串之后的子集 |- | [[jstl function substringbefore|fn:substringBefore()]] | 返回字符串在指定子串之前的子集 |- | [[jstl function tolowercase|fn:toLowerCase()]] | 将字符串中的字符转为小写 |- | [[jstl function touppercase|fn:toUpperCase()]] | 将字符串中的字符转为大写 |- | [[jstl function trim|fn:trim()]] | 移除首尾的空白符 |}
返回至“
JSP 标准标签库(JSTL)
”。
上一节:
JSP_发送邮件
下一节:
JSP_连接数据库