从 servlet 类读取 XML 文件并将其显示在 JSP 页面上

发布于 2024-10-27 09:31:28 字数 748 浏览 1 评论 0原文

首先我想知道我的做法是否正确。 我有存储在 xml 文件中的产品。我将读取该文件并存储产品,以便可以将其传递到 JSP 页面。这是我的 Product.xml 文件,

我还想知道我应该使用哪种数据结构。因为在产品页面上,每个产品都会有一个“添加到购物车”按钮。

<inventory>
<product>
    <name>Dictionairy</name>
    <description>Words from the English language</description>
    <price>400</price>

</product>
<product>
    <name>Calculator</name>
    <description>solving numbers</description>
    <price>1000</price>

</product>
<product>
    <name>LCD</name>
    <description>displaying output from computer</description>
    <price>8000</price>
</product>
</inventory>

First of all i would like to know whether my approach is correct or not.
I have products that are stored in a xml file. I will be reading this file, and storing Products, so that i can pass it to the JSP page. Here is my Product.xml file

I would also like to know, what sort of data structure I should use. Since on the products page, I will have an ADD TO CART button, for every product.

<inventory>
<product>
    <name>Dictionairy</name>
    <description>Words from the English language</description>
    <price>400</price>

</product>
<product>
    <name>Calculator</name>
    <description>solving numbers</description>
    <price>1000</price>

</product>
<product>
    <name>LCD</name>
    <description>displaying output from computer</description>
    <price>8000</price>
</product>
</inventory>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘东忘西忘不掉你 2024-11-03 09:31:28

解析 servlet 中的 XML 并将其转换为 JSP 可以使用的内存数据结构,听起来相当重量级。

将 XML 传递给 JSP,然后由 JSP 解析和遍历它听起来更糟糕。

如果 servlet 正在提取 kosher XML,并且呈现代码的任务只是过滤内容并将其呈现为 HTML,那么也许您应该使用 XSLT 处理器而不是 JSP 来进行呈现。

事实上,您甚至可以在 HTML 响应中发送带有嵌入处理指令的 XML,以告诉客户端要使用什么 XSL...如果需要的话。

Parsing the XML in the servlet and turning it into an in-memory data structure that the JSP can use sounds rather heavy-weight.

Passing XML to a JSP which then parses and traverses it sounds even worse.

If the servlet is extracting kosher XML, and the task of the rendering code is simply to filter and render the content as HTML, then maybe you should be looking at an XSLT processor rather than a JSP to do the rendering.

Indeed, you could even send the XML in the HTML response with an embedded processing directive to tell the client-side what XSL to use ... if it wants to.

反目相谮 2024-11-03 09:31:28

好吧,作为一个快速的镜头,我会选择一个包含代表所有可能产品的 Product 类集合的 Inventory 类。然后有一个 Cart 类,它还可以包含产品(对产品的引用)以及数量(这实际上可能是 CartPositionCart > 持有职位)。

请注意,Inventory 应存储在应用程序范围内,因为它对于每个客户来说似乎都是相同的。请记住,对清单的访问可能需要同步,或者至少应该锁定/隐藏,直到完全读取。另一方面,Cart 可能存储在会话中。

另请注意,这是一种非常简单的方法。开一家真正的商店需要做的事情还有很多。

Well, as a quick shot I'd go for a class Inventory containing a collection of Product classes which represent all possible products. Then have a class Cart which can also contain products (references to the products) as well as a quantity (this might actually be a CartPosition and Cart holds positions).

Note that Inventory should be stored in application scope, since it seems to be the same for each customer. Keep in mind that access to the inventory might need to be synchronized or it should at least be locked/hidden until fully read. Cart on the other hand might be stored in the session.

Also note that this is a very simple approach. Doing a real shop involves a lot more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文