为了账号安全,请及时绑定邮箱和手机立即绑定

删除没有反应

package servlet;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import entity.itemsDAO;

import entity.Cart;

import entity.items;


public class CartServlet extends HttpServlet {


private String action ; //表示购物车的动作 ,add,show,delete

//商品业务逻辑类的对象

private itemsDAO idao = new itemsDAO();



/**

* Constructor of the object.

*/

public CartServlet() {

super();

}


/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}


/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

        doPost(request,response);

}


/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


response.setContentType("text/html;charset=utf-8");

PrintWriter out = response.getWriter();

if(request.getParameter("action")!=null)

{

this.action = request.getParameter("action");

if(action.equals("add")) //如果是添加商品进购物车

{

if(addToCart(request,response))

{

request.getRequestDispatcher("/success.jsp").forward(request, response);

}

else

{

request.getRequestDispatcher("/failure.jsp").forward(request, response);

}

}

if(action.equals("show"))//如果是显示购物车

{

request.getRequestDispatcher("/cart.jsp").forward(request, response);

}

if(action.equals("delete")) //如果是执行删除购物车中的商品

{

if(deleteFromCart(request,response))

{

request.getRequestDispatcher("/cart.jsp").forward(request, response);

}

else

{

request.getRequestDispatcher("/cart.jsp").forward(request, response);

}

}

}


}


//添加商品进购物车的方法

private boolean addToCart( HttpServletRequest request, HttpServletResponse response)

{

String id = request.getParameter("id");

String number = request.getParameter("num");

items item = idao.getItemsById(Integer.parseInt(id));


//是否是第一次给购物车添加商品,需要给session中创建一个新的购物车对象

if(request.getSession().getAttribute("cart")==null)

{

Cart cart = new Cart();

request.getSession().setAttribute("cart",cart);

}

Cart cart = (Cart)request.getSession().getAttribute("cart");

if(cart.CartAdd(item, Integer.parseInt(number)))

{

return true;

}

else

{

return false;

}


}


//从购物车中删除商品

private boolean deleteFromCart(HttpServletRequest request, HttpServletResponse response){

String id = request.getParameter("id");

Cart cart = (Cart) request.getSession().getAttribute("cart");

items it = idao.getItemsById(Integer.parseInt(id));

System.out.println(it.getName());

if(cart.CartRemove(it))

   {

    return true;

   }

   else

   {

    return false;

   }



}


/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}


}

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

<%@ page import="entity.Cart" %>

<%@ page import="entity.items" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    <title>My JSP 'cart.jsp' starting page</title>

  <meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

    <link type="text/css" rel="stylesheet" href="css/style1.css" />

    <script language="javascript">

   function delcfm() {

       if (!confirm("确认要删除?")) {

           window.event.returnValue = false;

       }

   }

   </script>

  </head>

  

  <body>

   <h1>我的购物车</h1>

   <a href="index.jsp">首页</a> >> <a href="index.jsp">商品列表</a>

   <hr> 

   <div id="shopping">

   <form action="" method="">

<table>

<tr>

<th>商品名称</th>

<th>商品单价</th>

<th>商品价格</th>

<th>购买数量</th>

<th>操作</th>

</tr>

<% 

  //首先判断session中是否有购物车对象

  if(request.getSession().getAttribute("cart")!=null)

  {

%>

<!-- 循环的开始 -->

    <% 

        Cart cart = (Cart)request.getSession().getAttribute("cart");

        HashMap<items,Integer> goods = cart.getGoods();

        Set<items> items = goods.keySet();

        Iterator<items> it = items.iterator();

        

        while(it.hasNext())

        {

           items i = it.next();

    %> 

<tr name="products" id="product_id_1">

<td class="thumb"><img src="images/<%=i.getPicture()%>" /><a href=""><%=i.getName()%></a></td>

<td class="number"><%=i.getPrice() %></td>

<td class="price" id="price_id_1">

<span><%=i.getPrice()*goods.get(i) %></span>

<input type="hidden" value="" />

</td>

<td class="number">

                      <%=goods.get(i)%>

</td>                        

                    <td class="delete">

 <a href="servlet/CartServlet?action=delete&id=<%=i.getId()%>"  onclick="delcfm();">删除</a>                  

</td>

</tr>

    <% 

        }

    %>

<!--循环的结束-->


</table>

<div class="total"><span id="total">总计:<%=cart.CartTotal() %>¥</span></div>

 <% 

   }

%>

<div class="button"><input type="submit" value="" /></div>

</form>

</div>

  </body>

</html>

package entity;


import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;


//购物车类

public class Cart {

private HashMap<items, Integer> goods; //商品类和数量集合

private double total;//商品总价

//构造方法

public Cart(){

goods = new HashMap<items, Integer>();

total = 0.0;

}


public HashMap<items, Integer> getGoods() {

return goods;

}


public void setGoods(HashMap<items, Integer> goods) {

this.goods = goods;

}


public double getTotal() {

return total;

}


public void setTotal(double total) {

this.total = total;

}


//添加商品和商品数量进购物车 的方法

public boolean CartAdd(items i,int n){

if(goods.containsKey(i)){

goods.put(i, goods.get(i)+n);

}else{

goods.put(i, n);

}

CartTotal();//每次添加后重新计算总价格

return  true;

}

//删除商品的方法

public boolean CartRemove(items i){

goods.remove(i);

CartTotal();//每次删除后重新计算总价格

return true;

}

//统计购物车内物品的总价格

public double CartTotal(){

double d = 0.0;

Set<items> keys = goods.keySet();//获取键值对集合

Iterator<items> it = keys.iterator();//获得迭代器对象

while (it.hasNext()){

items i = it.next();

d += i.getPrice()* goods.get(i);

}

this.setTotal(d);

return this.getTotal(); //返回总价格


}

// public static void main(String[] args) {

// //先创建两个商品对象

// items t1 = new items(1,"沃特篮球鞋","温州",200,500,"001.jpg");

// items t2 = new items(2,"李宁运动鞋","广州",300,500,"002.jpg");

// items t3 = new items(1,"沃特篮球鞋","温州",200,500,"001.jpg");

//

// //实例化购物车类

// Cart c = new Cart();

// c.CartAdd(t1, 3);

// c.CartAdd(t2, 1);

// c.CartAdd(t3, 1);

// c.CartRemove(t1);

// //遍历购物车商品类集合

// Set<Map.Entry<items, Integer>> items =  c.getGoods().entrySet();

// for(Map.Entry<items, Integer> obj: items){

// System.out.println(obj);

// }

//

// System.out.println("商品总价为:"+c.getTotal());

//

// }

}



正在回答

1 回答

检查以下点删除按钮是的提交路径,在Cart.jsp代码里面

<td class="delete">

 <a href="servlet/CartServlet?action=delete&id=<%=i.getId()%>" onclick="delcfm();">删除</a>                

</td>


1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

删除没有反应

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信