如何将从 JSP 请求的 XML 数据格式化为表
我有一个 .jsp,它确定用户可以访问哪些特定内容。它创建一个 XML 文件,该文件由前面的 jsp 读入流中。如何使用读入 char 数组流的 XML 数据填充表?
I have a .jsp which determines what specific things a user has access to. It creates an XML file which is read into a stream by the previous jsp. How would I populate a table with the XML data which is read into a char array stream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有很多方法可以从 XML 文件中获取 HTML 表格。最简洁的方法之一是将 XML 解析为可重用的 javabeans 集合,并将其传递给 JSP,以便您可以使用 JSTL
在渲染 HTML 表时对其进行迭代。这样,每一层都有自己明确的职责。 Java SE 提供的 JAXB 对此非常有帮助。想象一下您的 XML 如下所示:
然后您可以创建一个 JAXB javabean,如下所示:
然后您可以将其转换为
List
,如下所示:然后您可以让您的 servlet (或 JSP?:/ )在将请求转发到 JSP 之前将其存储在请求范围中:
最后,您可以在 JSP 中迭代它并呈现它作为一个HTML 表格:
There are a lot of ways to get a HTML table out of the XML file. One of the most clean ways is to parse that XML into a collection of reuseable javabeans which you pass to the JSP, so that you can use JSTL
<c:forEach>
to iterate over it while rendering a HTML table. This way every layer keeps its own clear responsibility. The Java SE provided JAXB is very helpful in this.Imagine that your XML look like this:
Then you can create a JAXB javabean as follows:
Then you can transform it into a
List<Entry>
as follows:Then you can let your servlet (or JSP? :/ ) store it in the request scope before forwarding the request to the JSP:
Finally you can in JSP iterate over it and present it as a HTML table:
您可以使用 XSLT 按原样设置 XML 样式,或者使用 XPath 浏览 XML 并构建适合您需要的表。
但不确定嵌入该逻辑是否干净,我宁愿在 bean 中处理它,并且您可以从 jsp 可用于生成表的 bean 中获取最终列表。
http://oreilly.com/catalog/javaxslt/chapter/ch05.html
You can use XSLT to style the XML as it is or use XPath to navigate over the XML and build an table that fits your needs.
But not sure if embedding that logic is clean, I would rather that was processed in a bean and you could get the final list from a bean that the jsp can use to produce the table.
http://oreilly.com/catalog/javaxslt/chapter/ch05.html
那么在您的 JSP 中除了作为原始 XML 文档的 InputStream 之外什么都没有吗?在我看来,除了学习处理 XML 的 Java 库之外,您别无选择。
Google 搜索“Java 中的 XML 处理”,您将看到无数可用的选项。即使是上面的“将 XML 转换为可以在页面的 HTML 中处理的内容的 XSLT”建议也将要求您解析此流。 JAXB API (http://jaxb.java.net/tutorial/section_1_2-Overview.html#Overview) 使这件事变得不那么痛苦,允许您将 XML 文档映射到 POJO。
不管怎样,你会发现在 JSP 级别处理这个有点代码味道。您应该在其他地方处理 XML 流并将 POJO 结果发送给您的 JSP 以供演示。
我的2分钱值。
So in your JSP you've got nothing other than the InputStream that is the raw XML document? It seems to me that you have little choice but to learn the Java libraries for processing XML.
Google search "XML Processing in Java" and you'll see the myriad of options available to you. Even the "XSLT to transform your XML to something you can deal with in the HTML of the page" recommendation above is going to require you to parse this stream. The JAXB API (http://jaxb.java.net/tutorial/section_1_2-Overview.html#Overview) makes this a little less painful, allowing you to map the XML document into POJOs.
Anyway you look at it, processing this at the JSP level is a bit of a code smell. You should process the XML stream somewhere else and send your JSP the POJO results for presentation.
My 2 cents worth.