Java 中的 servlet
我想要的是显示一个产品列表,每个产品都有一个“添加到购物车”按钮。我如何将该按钮与产品关联起来?
即,如果我单击将项目 A 添加到购物车,我将如何在按钮中指定调用 ShoppingCart 类,从哪里添加该项目。
我已经有一个 servlet,用于将所有产品填充到 ArrayList 中,并将其转发到 JSP 页面。
<form action="products" method="get">
<table border=1>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.id}</td>
<td>${product.description}</td>
<td>${product.unitPrice}</td>
<td><input type="button" value="Add to cart" onlick="????"/></td>
</tr>
</c:forEach>
</table>
</form>
What I want is to display a list of products with an "add to cart" button for each product. How will I associate that button with reference to the product?
i.e If I click to add an Item A to the cart, how will I specify in the button, to call the ShoppingCart class, from where the item will be added.
I already have a servlet, for populating all the products into an ArrayList, and the forward it to a JSP page.
<form action="products" method="get">
<table border=1>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.id}</td>
<td>${product.description}</td>
<td>${product.unitPrice}</td>
<td><input type="button" value="Add to cart" onlick="????"/></td>
</tr>
</c:forEach>
</table>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将表单放在
中,而不是放在桌子周围。使用
让按钮真正发挥作用,而不需要 JavaScript 破解,而 JavaScript 破解在禁用 JS 的网络浏览器上无论如何都会失败。在表单中包含以传递产品 ID。
总结:
这样,它将在监听
/products
的 servlet 的doGet()
方法中可用,如下所示。与具体问题无关,您希望使用 POST 而不是 GET 来实现此目的。否则,当用户为结果页面添加书签并稍后打开它时,或者当她/他只是单击/跟随另一个人传递的链接(潜在的恶意)电子邮件/即时消息/网站/等中的人。将
method="get"
替换为method="post"
并将doGet()
中的逻辑移至doPost()
>。Put the form in the
<td>
instead of around the table. Use<inupt type="submit">
to let the button really function without the need for JavaScript hacks which would fail anyway on webbrowsers with JS disabled. Include a<input type="hidden">
in the form to pass the product ID.Summarized:
This way it'll be available as follows in
doGet()
method of the servlet listening on/products
.Unrelated to the concrete problem, you'd like to use POST for this instead of GET. Otherwise the user will be able to add another product to his basket when s/he bookmarks the result page and opens it later, or when s/he just clicks/follows a link passed by another (potential malicious) person in an email/IM/site/etc. Replace
method="get"
bymethod="post"
and move logic indoGet()
todoPost()
.它将 GET 到 servlet。从那里你可以阅读
It will make GET to servlet. and from there you can read
我已经从事 jsp 工作很多年了,但我可以给您概念并将实现留给您。
been ages tat i have worked on jsp's but i can give you the concept and leave the implementation to you.
您可以使用产品 id 调用一个函数:
然后 ajax 调用
servlet 将类似于
You would call a function using the product id:
and then an ajax call
the servlet would be something like
我对这些答案的怀疑是,在 TR 或 TD 内的每种表单中,都会出现整页发布请求,这对最终用户来说将是性能问题。每次单击“添加到购物车”按钮时,他都必须等待页面加载。您可以做的是调用 ajax 函数并使用 ITEM_ID 发布 URL,这样就不会导致您发布整页内容。要启用购物车条目,请使用内联 JSP,该 JSP 将使用表单发布进行刷新,以便获取购物车条目的详细信息。
或者,您可以调用 JSP 函数将项目添加到数组中,提交请求时将向该数组传递 Session 对象。有很多帖子可以让您更好地了解此实现。
使用 scriptlet 调用 onClick 函数
My doubt around these answers is that in each form within TR or TD there will be full page post request will occur that will be performance issue for the end user. He has to wait for page load every time he clicked the Add to cart button. What you can do is call an ajax function and post the URL with the ITEM_ID so that it will not cause you full page post. To enable the cart entry have a inline JSP that will be refreshed with the Form Post in order to get the details of the Cart entries.
Alternatively You can call a JSP function to add the Items in the Array that Will be passed a Session object while submitting the request. There is lot of posts are present to Give you better understanding of this implementation.
onClick function call using scriptlet