从 servlet 类读取 XML 文件并将其显示在 JSP 页面上
首先我想知道我的做法是否正确。 我有存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解析 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.
好吧,作为一个快速的镜头,我会选择一个包含代表所有可能产品的
Product
类集合的Inventory
类。然后有一个Cart
类,它还可以包含产品(对产品的引用)以及数量(这实际上可能是CartPosition
和Cart
> 持有职位)。请注意,
Inventory
应存储在应用程序范围内,因为它对于每个客户来说似乎都是相同的。请记住,对清单的访问可能需要同步,或者至少应该锁定/隐藏,直到完全读取。另一方面,Cart
可能存储在会话中。另请注意,这是一种非常简单的方法。开一家真正的商店需要做的事情还有很多。
Well, as a quick shot I'd go for a class
Inventory
containing a collection ofProduct
classes which represent all possible products. Then have a classCart
which can also contain products (references to the products) as well as a quantity (this might actually be aCartPosition
andCart
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.