💡 핵심 요약 (TL;DR)
Servlet장바구니3 : 장바구니리스트, 장바구니에서 상품삭제의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* request에 저장할때 : 두 줄 코드를 한줄코드로 작성할 수 있어야한다.
BasketFrontController.java의 doProcess()의 주소비교 후 처리부분에 코드 추가1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 }else if (command.equals("/BasketList.ba" )){ System.out.println("C: /BasketList.ba 호출" ); action = new BasketListAction (); try { forward = action.execute(request, response); } catch (Exception e) { e.printStackTrace(); } }else if (command.equals("/BasketDelete.ba" )){ System.out.println("C: /BasketDelete.ba 호출" ); action = new BasketDeleteAction (); try { forward = action.execute(request, response); } catch (Exception e) { e.printStackTrace(); } }
BasketListAction.java 생성
List 2개를 한꺼번에 가져가는 방법은?
한 개의 리스트안에 두 개의 리스트를 넣으면 된다!
리스트가 여러개이면 헷갈리니까 여기서는 Vector 로 받을 것이다.
Vector와 ArrayList의 차이점은 동기화! 자세한 건 위의 링크를 참고
request에 저장할때 : 두 줄 코드를 한줄코드로 작성할 수 있어야한다.
1 2 3 4 5 6 List <BasketDTO> basketList = (List<BasketDTO>) totalData.get(0 );request.setAttribute("basketList" , basketList); request.setAttribute("basketList" , totalData.get(0 ));
request에 저장할때 원래 정보는 벡터에 저장되어 있지만, Action페이지에서 벡터의 정보를 꺼내서 각각 request에 저장해서 전달
이유1: view에서는 복잡한 코드가 있으면 안된다. View에서는 복잡한 연산을 피해야함. 바로 사용가능한 List형태로 전달하는것이 좋다.
1 2 3 4 5 6 request.setAttribute("totalData" , totalData); request.setAttribute("basketList" , totalData.get(0 )); request.setAttribute("goodsList" , totalData.get(1 ));
전체코드 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class BasketListAction implements Action { @Override public ActionForward execute (HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("M : BasketListAction의 execute() 호출" ); HttpSession session = request.getSession(); String id = (String) session.getAttribute("id" ); ActionForward forward = new ActionForward (); if (id == null ){ forward.setPath("./MemberLogin.me" ); forward.setRedirect(false ); return forward; } request.setCharacterEncoding("UTF-8" ); BasketDAO bkdao = new BasketDAO (); Vector totalData = bkdao.getBasketList(id); request.setAttribute("basketList" , totalData.get(0 )); request.setAttribute("goodsList" , totalData.get(1 )); forward.setPath("./goods_order/goods_basket.jsp" ); forward.setRedirect(false ); return forward; } }
BasketDAO.java 생성 후 getBasketList()메서드 코드 추가1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 public Vector getBasketList (String id) { Vector totalData = new Vector (); List <GoodsDTO> goodsList = new ArrayList <GoodsDTO>(); List <BasketDTO> basketList = new ArrayList <BasketDTO>(); try { getCon(); sql ="select * from itwill_basket where b_m_id=?" ; pstmt = con.prepareStatement(sql); pstmt.setString(1 , id); rs = pstmt.executeQuery(); while (rs.next()){ System.out.println(">>> 바스켓 rs while시작" ); BasketDTO bkdto = new BasketDTO (); bkdto.setB_date(rs.getDate("b_date" )); bkdto.setB_g_amount(rs.getInt("b_g_amount" )); bkdto.setB_g_color(rs.getString("b_g_color" )); bkdto.setB_g_num(rs.getInt("b_g_num" )); bkdto.setB_g_size(rs.getString("b_g_size" )); bkdto.setB_m_id(rs.getString("b_m_id" )); bkdto.setB_num(rs.getInt("b_num" )); basketList.add(bkdto); sql ="select * from itwill_goods where gno=?" ; PreparedStatement pstmt2 = con.prepareStatement(sql); pstmt2.setInt(1 , bkdto.getB_g_num()); ResultSet rs2 = pstmt2.executeQuery(); if (rs2.next()){ System.out.println(">>> 굿즈 rs if시작" ); GoodsDTO gdto = new GoodsDTO (); gdto.setImage(rs2.getString("image" )); gdto.setName(rs2.getString("name" )); gdto.setPrice(rs2.getInt("price" )); goodsList.add(gdto); } System.out.println(">>> 굿즈 rs if끝" ); } System.out.println(">>> 바스켓 rs while끝" ); System.out.println("상품 정보 : " +goodsList); System.out.println("장바구니 정보 : " +basketList); totalData.add(basketList); totalData.add(goodsList); System.out.println("백터 정보 확인 : " +totalData); }catch (Exception e){ e.printStackTrace(); }finally { closeDB(); } return totalData; }
goods_basket.jsp 생성1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <% //한글처리 request.setCharacterEncoding("UTF-8"); //basketList에서 구현한 정보를 전달받아서 처리 List basketList = (List) request.getAttribute("basketList"); List goodsList = (List) request.getAttribute("goodsList"); BasketDTO bk = (BasketDTO) basketList.get(0); %> <table border ="1" > <caption > <%=bk.getB_m_id() %>님의 장바구니</caption > <tr > <td > 번호</td > <td > 사진</td > <td > 상품명</td > <td > 크기</td > <td > 색상</td > <td > 수량</td > <td > 가격</td > <td > 삭제</td > </tr > <% //EL태그는 FOR문과 상관없이 <c:forEach > 사용해서 출력해야한다. for(int i=0; i<basketList.size(); i++){ BasketDTO bkdto = (BasketDTO) basketList.get(i); GoodsDTO gdto = (GoodsDTO) goodsList.get(i); %> <tr > <td > <%=i+1%></td > <td > <img src ="./upload/<%=gdto.getImage().split(" ,")[0 ] %> " height="100px"></td > <td > <%=gdto.getName() %></td > <td > <%=bkdto.getB_g_size() %></td > <td > <%=bkdto.getB_g_color() %></td > <td > <%=bkdto.getB_g_amount() %></td > <td > <%=gdto.getPrice() %></td > <td > <input type ="button" class ="btn" value ="[장바구니에서 삭제]" onclick ="location.href='./BasketDelete.ba?b_num=<%=bkdto.getB_num()%>'" > </td > </tr > <%} %> </table > <input type ="button" class ="btn" value ="[구매하기]" onclick ="location.href='./OrderStar.or'" > <input type ="button" class ="btn" value ="[계속 쇼핑하기]" onclick ="location.href='./GoodsList.go'" >
BasketDeleteAction.java 생성1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class BasketDeleteAction implements Action { @Override public ActionForward execute (HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("M : BasketDeleteAction의 execute() 호출" ); int b_num = Integer.parseInt(request.getParameter("b_num" )); BasketDAO bkdao = new BasketDAO (); bkdao.deleteBasket(b_num); System.out.println("M: 장바구니에서 삭제 완료" ); ActionForward forward = new ActionForward (); forward.setPath("./BasketList.ba" ); forward.setRedirect(true ); return forward; } }
#TIL ITWILL(JAVA)