在JSP页面中使用标签库代替传统Java片段语文来实现页面显示逻辑已经不是新技术了,但是自定义的标签容易造成重复定义和非标准的实现,所以JSTL(JSP Standard Tag Library,JSP标准标签库)诞生了。JSTL是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的(标准是SUN制定,apache实现的,SUN收录了apache的实现)。JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上。
Version | JSTL version | Requirements |
---|---|---|
Standard 1.2.3 | JSTL 1.2 | Servlet 2.5, JavaServer Pages 2.1 |
Standard 1.1 | JSTL 1.1 | Servlet 2.4, JavaServer Pages 2.0 |
Standard 1.0 | JSTL 1.0 | Servlet 2.3, JavaServer Pages 1.2 |
Sun 发布的标准 JSTL1.1 标签库有以下几个标签:
- 核心标签库:包含 Web 应用的常见工作,比如:循环、表达式赋值、基本输入输出等。
- 格式化标签库:用来格式化显示数据的工作,比如:对不同区域的日期格式化等。
- 数据库标签库:可以做访问数据库的工作。
- XML 标签库:用来访问 XML 文件的工作,这是 JSTL 标签库的一个特点。
- 函数标签库:用来读取已经定义的某个函数。
使用JSTL的步骤:
- 在项目中引入jstl的相关jar包,确保jar存在:jstl.jar、standard。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
- 在需要使用标签的JSP页面上使用taglib指令引入标签库。
- 在需要使用标签的位置直接使用标签库。
XML标签:
在使用xml标签前,你必须将XML 和 XPath 的相关jar包也引入项目:
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.1</version>
</dependency>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSTL_XML</title>
</head>
<body>
<hr>
<!-- ****解析XML【x:parse】****
var="存储XML的变量"
xml="需要解析的文档内容(String或Reader),默认为主体内容"
systemId="系统标识符URL,用来解析文档"
filter="应用于源文档的过滤器"
doc="需要解析的xml文档"
varDom="包含已解析XML数据的变量"
scope="varDom属性的作用域(page request session application),默认为page"
scope="var属性的作用域(page request session application),默认为page"
-->
<x:parse var="output">
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
</x:parse>
<!-- 输出:
以上解析正确执行
-->
<hr>
<!-- ****显示数据【x:out】****
select="XPath表达式"
escapeXml="是否忽略XML特殊字符,默认为true"
-->
<x:out select="$output/books/book[1]/name" />
、
<x:out select="$output/books/book[1]/price" />
<!-- 输出:
Padam History 、 100
-->
<hr>
<!-- ****设置变量【x:set】****
var="代表XPath表达式值的变量"
select="XPath表达式,默认为主体内容"
scope="var属性的作用域(page request session application),默认为page"
-->
<x:set select="$output/books/book[1]/name" var="name1"/>
<x:set select="$output/books/book[1]/price" var="price1"/>
${name1}、${price1}
<!-- 输出:
[]、[]
-->
<hr>
<!-- ****判断【x:if】****
var="存储条件结果的变量,必填项"
select="需要计算的XPath表达式"
scope="var属性的作用域(page request session application),默认为page"
-->
<x:if select="$output//book">
Document has at least one <book> element.
</x:if>
<br />
<x:if select="$output/books[1]/book/price > 1000">
Book prices are very high
</x:if>
<!-- 输出:
Document has at least one element.
Book prices are very high
-->
<hr>
<!-- ****循环遍历【x:forEach】****
select="需要计算的XPath表达式,必填项"
var="存储当前项目的变量"
begin="迭代器的开始索引"
end="迭代器的结束索引"
step="迭代的步长"
varStatus="代表迭代器所存储的状态的变量"
varStatus的属性有:
index 当前条目从0开始的迭代索引
count 当前条目从1开始的迭代计数
first 用来表明当前这轮迭代是否为第一次迭代的标志
last 用来表明当前这轮迭代是否为最后一次迭代的标志
-->
<x:forEach select="$output/books/book/name" var="item" varStatus="varStatus">
<x:out select="$item"/>、
${varStatus.index}、${varStatus.count}、${varStatus.first}、${varStatus.last}
<br>
</x:forEach>
<!-- 输出:
Padam History、 0、1、true、false
Great Mistry、 1、2、false、true
-->
<hr>
<!-- ****模拟if..else if..else..【x:choose x:when x:otherwise】
$output//book/author 会拿匹配的所有数据去比较。
如果把两个when条件换下位置结果就是“Book is written by NUHA”了
**** -->
<x:choose>
<x:when select="$output//book/author = 'ZARA'">
Book is written by ZARA
</x:when>
<x:when select="$output//book/author = 'NUHA'">
Book is written by NUHA
</x:when>
<x:otherwise>
Unknown author.
</x:otherwise>
</x:choose>
<!-- 输出:
Book is written by ZARA
-->
<hr>
<!-- ****应用XSL【x:transform】****
XSL(可扩展样式表语言)是XML的样式表,用来可描述如何来显示XML文档!XSL 之于XML,就像CSS之于HTML。
doc="源XML文档,默认为主体内容"
docSystemId="源XML文档的URI"
xslt="XSLT样式表,必填项"
xsltSystemId="源XSLT文档的URI"
result="接收转换结果的对象,默认直接显示在当前页面"
var="代表被转换的XML文档的变量,默认直接显示在当前页面"
scope="var属性的作用域(page request session application),默认为page"
-->
<c:set var="xmltext">
<books>
<book>
<name>Padam History</name>
<author>ZARA</author>
<price>100</price>
</book>
<book>
<name>Great Mistry</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
</c:set>
<c:set var="xslt">
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="books">
<table border="1" width="100%">
<xsl:for-each select="book">
<tr>
<td>
<i>
<xsl:value-of select="name" />
</i>
</td>
<td>
<xsl:value-of select="author" />
</td>
<td>
<xsl:value-of select="price" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
</c:set>
<x:transform xml="${xmltext}" xslt="${xslt}"></x:transform>
<!-- 输出:
按表格的样式显示数据
-->
<hr>
<!-- ****设置XSLT参数【x:param】****
name="XSLT参数的名称,必填项,默认为主体内容"
value="XSLT参数的值"
-->
<c:set var="xslt_">
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:param name="bgColor"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="books">
<table border="1" width="50%" bgColor="{$bgColor}">
<xsl:for-each select="book">
<tr>
<td>
<i><xsl:value-of select="name"/></i>
</td>
<td>
<xsl:value-of select="author"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
</c:set>
<x:transform xml="${xmltext}" xslt="${xslt_}">
<x:param name="bgColor" value="grey"/>
</x:transform>
<!-- 输出:
按表格的样式显示数据,并且背景色为grey
-->
</body>
</html>