【发布时间】:2012-10-22 16:34:37
【问题描述】:
我想知道是否可以将 xml 文档传递给 pure jstl 定义的 JSP 自定义标记,例如: 自定义标签的正文,如:
<mt:mytag>
<people>
<person name="bob" age="23" />
<person name="sue" age="45" />
<person name="moe" age="35" />
</people>
<mt:mytag>
或者像这样作为标签的属性:
<mt:mytag message="http://link.to.document.xml" />
这是标签本身
<%@tag description="xml parser" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<%@attribute name="message"%> OR <jsp:doBody var="message" />
<x:parse var="a" doc="${message}" />
<x:forEach var="current" select="$a/people/person">
<ul>
<li>
Name <x:out select="$current/@name" /> age <x:out select="$current/@age" />
</li>
</ul>
</x:forEach>
可以从jsp页面内部使用jstl处理xml,基本上是在forEach之后复制代码并粘贴到jsp中。它甚至可以在页面请求中将 xml 作为 POST/GET 参数获取并在页面中进行处理。
否则在做上面的例子的时候,就会出现各种这样的错误:
PWC6197: An error occurred at line: 9 in the jsp file: /WEB-INF/tags/test2.tag
PWC6199: Generated servlet error:
cannot access javax.servlet.jsp.jstl.core.LoopTagSupport
class file for javax.servlet.jsp.jstl.core.LoopTagSupport not found
PWC6197: An error occurred at line: 9 in the jsp file: /WEB-INF/tags/test2.tag
PWC6199: Generated servlet error:
cannot find symbol
symbol: method setPageContext(javax.servlet.jsp.PageContext)
location: variable _jspx_th_x_forEach_0 of .......
请注意,在非纯 JSTL(使用 java 代码)中处理正文内容或属性链接是完全可能的,只是想知道 JSTL+EL 是否有这样的功能。
编辑:分辨率
看起来 Netbeans IDE 有一个 bug,默认情况下它不添加 JSTL 库。您可以通过 Libraries->Add Library->Import->Jstl 1.1->Add Library
修复它【问题讨论】:
标签: java xml jsp jakarta-ee jstl