前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用JSP的标准标签库JSTL处理XML格式的数据

使用JSP的标准标签库JSTL处理XML格式的数据

作者头像
Jerry Wang
发布2020-07-21 11:16:00
1K0
发布2020-07-21 11:16:00
举报

JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。

下载地址:https://archive.apache.org/dist/jakarta/taglibs/standard/binaries/

下载文件jakarta-taglibs-standard-1.1.2.zip,将lib文件夹下的jstl.jar和standard.jar文件,置于JSP项目的WEB-INF/lib下。

在web.xml中添加如下内容:

代码语言:javascript
复制
<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文件中的头部包含标签。

在WebContent文件夹下新建一个books.xml文件,内容如下:

代码语言:javascript
复制
<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>

浏览器里可以立即看到这个xml文件:

main.jsp的源代码:

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
 
<html>
<head>
  <title>JSTL x:parse Tags</title>
</head>
<body>
<h3>Books Info:</h3>
<c:import var="bookInfo" url="http://localhost:9032/jerryjsp/books.xml"/>
 
<x:parse xml="${bookInfo}" var="output"/>
<b>The title of the first book is</b>: 
<x:out select="$output/books/book[1]/name" />
<br>
<b>The price of the second book</b>: 
<x:out select="$output/books/book[2]/price" />
 
</body>
</html>

为了能够处理xml格式的数据,tomcat lib文件夹下面必须要有这些jar包:

  • xalan.jar
  • xercesImpl.jar
  • serializer.jar
  • xml-apis.jar

最后的结果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档