问题
I wonder if it's possible to pass an xml document to a pure jstl-defined JSP custom tag either as: the body of the custom tag, such as:
<mt:mytag>
<people>
<person name="bob" age="23" />
<person name="sue" age="45" />
<person name="moe" age="35" />
</people>
<mt:mytag>
or as an attribute of the tag like this:
<mt:mytag message="http://link.to.document.xml" />
This is the tag itself
<%@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>
It is possible to process an xml with jstl from inside a jsp page, basically copying the code after forEach and pasting in the jsp. It works even to get the xml as a POST/GET parameter in the page request and process it in the page.
Otherwise, when doing the above examples, there are various errors of this kind:
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 .......
Please note that it's perfectly possible to process either the body content or an attribute link inside a non-pure JSTL (with java code), just wondering if JSTL+EL had such facilities.
Edit: Resolution
Looks like the Netbeans IDE has a bug where it does not add the JSTL libraries by default. You fix it by Libraries->Add Library->Import->Jstl 1.1->Add Library
回答1:
This works.
<%@ taglib tagdir="/WEB-INF/tags" prefix="mt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<c:set var="message">
<people>
<person name="bob" age="23" />
<person name="sue" age="45" />
<person name="moe" age="35" />
</people>
</c:set>
<mt:mytag message="${message}" />
Actually, your first code block(using body) works for me as well. If you want to use a file, then use the following.
<%@ taglib tagdir="/WEB-INF/tags" prefix="mt" %>
<mt:mytag messageUrl="http://link.to.document.xml" />
and tag file;
<%@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="messageUrl"%>
<c:import url="${messageUrl}" 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>
In regard to your error messages, please tell us which web server you are using. Also tell us which JSTL jars you downloaded and from where.
来源:https://stackoverflow.com/questions/13188092/pass-xml-document-via-attribute-or-body-to-jsp-custom-tag