回 帖 发 新 帖 刷新版面

主题:[原创]JSTL 解析 XML的奇怪问题

我已经把standard.jar,jstl.jar放到了lib下,所有的tld文件也已经放到tlds目录下,
并已经在web.xml中声明如下:
<?xml version="1.0" encoding="utf-8"?>
<web-app>
<display-name>JSTL</display-name>
<description>JSTL samples described in the book </description>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/tlds/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tlds/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/tlds/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/tlds/x.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/functions</taglib-uri>
<taglib-location>/WEB-INF/tlds/fn.tld</taglib-location>
</taglib>
</web-app>

相应的JSP文件如下:
<%@page contentType="text/html;charset=GBK"%>
<%@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 Support for XML</title>
</head>
<body>
<c:set var="taskXML">
<tasklist name="WEBapplication task" manager="Zhang">
<task>
    <taskid>JOB01</taskid>
    <name>prepare the design requirement</name>
    <start>10/1/2002</start>
    <end>20/2/2003</end>
</task>
<task>
    <taskid>JOB02</taskid>
    <name>write design documents</name>
    <start>20/1/2003</start>
    <end>10/3/2003</end>
</task>
</tasklist>
</c:set>
<x:parse xml="${taskXML}" var="taskresult"/>
<center>
<h3>The tasklist using JSTL XML tags:</h3>
<table border="1" width="50%">
<tr>
<th>task ID</th>
<th>task name</th>
<th>start</th>
<th>finish</th>
</tr>
<x:forEach select="$taskresult/tasklist/task" var="sigletask">
<tr><td><x:out select="taskid"/></td>
<td><x:out select="name"/></td>
<td><x:out select="start"/></td>
<td><x:out select="end"/></td>
</tr>
</x:forEach>
</table>
</center>
</body>
</html>

执行时产生如下异常:
exception 

javax.servlet.ServletException: javax.servlet.jsp.JspException: Content is not allowed in prolog.
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:842)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.xmlTest_jsp._jspService(xmlTest_jsp.java:102)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause 

org.xml.sax.SAXParseException: Content is not allowed in prolog.
    org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    org.apache.taglibs.standard.tag.common.xml.ParseSupport.parseInputSource(ParseSupport.java:227)
    org.apache.taglibs.standard.tag.common.xml.ParseSupport.parseInputSourceWithFilter(ParseSupport.java:193)
    org.apache.taglibs.standard.tag.common.xml.ParseSupport.parseReaderWithFilter(ParseSupport.java:199)
    org.apache.taglibs.standard.tag.common.xml.ParseSupport.parseStringWithFilter(ParseSupport.java:206)
    org.apache.taglibs.standard.tag.common.xml.ParseSupport.doEndTag(ParseSupport.java:138)
    org.apache.jsp.xmlTest_jsp._jspx_meth_x_parse_0(xmlTest_jsp.java:165)
    org.apache.jsp.xmlTest_jsp._jspService(xmlTest_jsp.java:78)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

上网搜索了大量资料,大多说是什么.xml文件保存为utf-8格式时,会在头部加上隐藏的
几个编码而致使分析器辨认不了。但我用UltraEdit打开查看十六进制时并没有多余的编码。
各位新老手,帮帮小弟吧

回复列表 (共1个回复)

沙发

到底有无人懂的啊?
难道这个论坛真是无高手了吗?

我来回复

您尚未登录,请登录后再回复。点此登录或注册