清除Java中的购物车按钮

清除Java中的购物车按钮,java,html,css,jsp,project,Java,Html,Css,Jsp,Project,嗨,我正试图在我的java代码中创建一个“Clear Cart”按钮,但我遇到了困难。 我已经试了几个小时了,但还是没能成功 我认为这主要与第一部分有关,第一部分包含所有字符串和变量 代码如下: <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <%@ page errorPage="errorpage.jsp" %> <jsp:useBean id="cart"

嗨,我正试图在我的java代码中创建一个“Clear Cart”按钮,但我遇到了困难。 我已经试了几个小时了,但还是没能成功

我认为这主要与第一部分有关,第一部分包含所有字符串和变量

代码如下:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page errorPage="errorpage.jsp" %>
<jsp:useBean id="cart" scope="session" class="beans.ShoppingCart" />

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>the Game Store</title>
    </head>
    <body>
<%
    String id = request.getParameter("id");
    if ( id != null ) {
      String desc = request.getParameter("desc");

      Float price = new Float(request.getParameter("price"));

      cart.addItem(id, desc, price.floatValue(), 1);

    }
  %>
  <a href="ShoppingCart.jsp">Shopping Cart Quantity:</a>
    <%=cart.getNumOfItems() %>
  <hr>
  <center><h3>Games</h3></center>
  <table border="1" width="300" cellspacing="0"
  cellpadding="2" align="center">
    <tr><th>Description</th><th>Price</th></tr>
    <tr>
      <form action="AddToShoppingCart.jsp" method="post">
        <td>Goat Simulator</td>
        <td>$11.95</td>
        <td><input type="submit" name="Submit" value="Add"></td>
        <input type="hidden" name="id" value="1">
        <input type="hidden" name="desc" value="Goat Simulator">
        <input type="hidden" name="price" value="11.95">
      </form>
    </tr>
    <tr>
      <form action="AddToShoppingCart.jsp" method="post">
        <td>Metal Gear Solid V</td>
        <td>$59.99</td>
        <td><input type="submit" name="Submit" value="Add"></td>
        <input type="hidden" name="id" value="2">
        <input type="hidden" name="desc" value="Metal Gear Solid V">
        <input type="hidden" name="price" value="59.99">
      </form>
    </tr>

     <tr>
      <form action="AddToShoppingCart.jsp" method="post">
        <input type ="submit" name="empty" id="empty-button" value="Empty Cart"/>
      </form>
      <a href="AddToShoppingCart.jsp">Clear</a>

    </tr>

    </table>
  </body>
</html>

游戏商店

游戏 描述价格 山羊模拟器 $11.95 金属齿轮实心V $59.99
这是整页的第二页

<%@ page errorPage="errorpage.jsp" %>
<%@ page import="java.util.*" %>

<jsp:useBean id="cart" scope="session" class="beans.ShoppingCart" />
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <title>Shopping Cart Contents</title>
  </head>
  <body>
    <center>
    <table width="300" border="1" cellspacing="0"
      cellpadding="2" border="0">
      <caption><b>Shopping Cart Contents</b></caption>
      <tr>
        <th>Description</th>
        <th>Price</th>
        <th>Quantity</th>
      </tr>
    <%
      Enumeration e = cart.getEnumeration();
      String[] tmpItem;
      // Iterate over the cart
      while (e.hasMoreElements()) {
        tmpItem = (String[])e.nextElement();
        %>
        <tr>
          <td><%=tmpItem[1] %></td>
          <td align="center">$<%=tmpItem[2] %></td>
          <td align="center"><%=tmpItem[3] %></td>
        </tr>
        <%
      }
    %> 
    </table>
    </center>
    <a href="AddToShoppingCart.jsp">Back to Catalog</a>
  </body>
</html>

购物车内容
购物车内容
描述
价格
量
$

我们能看看你的豆子吗(
beans.ShoppingCart
)?嗨,谢谢!我已经将它添加到postI think中,在你的ShoppingCart类中,你可以创建一个函数
clearCart
,它可能类似于
return new ShoppingCart()
。要在同一页面内处理不同的操作,可以使用参数,如
AddToShoppingCart.jsp?action=add
AddToShoppingCart.jsp?action=clear
,然后根据不同的操作在购物车内调用不同的方法