通过属性或主体将XML文档传递给JSP自定义标记 [英] Pass XML document via attribute or body to JSP custom tag
问题描述
我想知道是否可以将xml文档传递给纯 jstl定义的JSP自定义标记,如下所示:
自定义标记的主体,例如:
< mt:mytag>
< people>
< person name =bobage =23/>
< person name =sueage =45/>
< person name =moeage =35/>
< / people>
< mt:mytag>
或作为标签的属性,如下所示:
< mt:mytag message =http://link.to.document.xml/>
这是标签本身
<%@ tag description =xml parserpageEncoding =UTF-8%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/coreprefix =c%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/xmlprefix =x%>
<%@ attribute name =message%> OR< jsp:doBody var =message/>
< x:parse var =adoc =$ {message}/>
< x:forEach var =currentselect =$ a / people / person>
< ul>
< li>
名称< x:out select =$ current / @ name/>年龄< x:out select =$ current / @ age/>
< / li>
< / ul>
< / x:forEach>
可以在jsp页面内处理带有jstl的xml,基本上是在forEach之后复制代码并在jsp中粘贴。它甚至可以在页面请求中将xml作为POST / GET参数获取并在页面中处理它。
否则,在执行上述示例时,有各种各样的这种错误:
PWC6197:jsp文件中第9行:9位错误:/ WEB-INF / tags / test2.tag
PWC6199:生成的servlet错误:
无法访问javax.servlet.jsp.jstl.core.LoopTagSupport
找不到javax.servlet.jsp.jstl.core.LoopTagSupport的类文件
PWC6197:jsp文件中的行:9发生错误:/ WEB-INF/tags/test2.tag
PWC6199:生成的servlet错误:
找不到符号
symbol:方法setPageContext(javax.servlet.jsp.PageContext)
location:变量_jspx_th_x_forEach_0 .......
请注意,完全可以处理非纯JSTL(使用java代码)中的正文内容或属性链接,只是想知道JSTL + EL是否有这样的功能。 / p>
编辑:分辨率
看起来Netbeans IDE有 bug ,默认情况下它不会添加JSTL库。您可以通过库 - >添加库 - >导入 - > Jstl 1.1->添加库来修复它
这样可以。
<%@ taglib tagdir =/ WEB-INF / tagsprefix =mt%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/coreprefix =c%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/xmlprefix =x%>
< c:set var =message>
< people>
< person name =bobage =23/>
< person name =sueage =45/>
< person name =moeage =35/>
< / people>
< / c:set>
< mt:mytag message =$ {message}/>
实际上,你的第一个代码块(使用正文)也适用于我。
如果您想使用文件,请使用以下内容。
<%@ taglib tagdir =/ WEB-INF / tagsprefix =mt%>
< mt:mytag messageUrl =http://link.to.document.xml/>
和标签文件;
<%@ tag description =xml parserpageEncoding =UTF-8%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/coreprefix =c%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/xmlprefix =x%>
<%@ attribute name =messageUrl%>
< c:import url =$ {messageUrl}var =message/>
< x:parse var =adoc =$ {message}/>
< x:forEach var =currentselect =$ a / people / person>
< ul>
< li>
名称< x:out select =$ current / @ name/>年龄< x:out select =$ current / @ age/>
< / li>
< / ul>
< / x:forEach>
关于您的错误消息,请告诉我们您使用的是哪个Web服务器。还要告诉我们您下载了哪些JSTL罐子以及从哪里下载。
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
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.
这篇关于通过属性或主体将XML文档传递给JSP自定义标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!