이 기사는 참조를 위해 Javaweb 서점의 쇼핑 카트 모듈을 계속 공유합니다. 특정 내용은 다음과 같습니다
쇼핑 카트 저장
쿠키에 세션 저장을 저장하여 데이터베이스에 저장하십시오
1. 관련 클래스를 만듭니다
쇼핑 카트 구조 :
Cartitem : 책과 수량이 포함 된 쇼핑 카트 입장
카트 : 지도를 포함한 쇼핑 카트
/*** 쇼핑 카트 클래스*/public class cart {private map <string, cartitem> map = new LinkedHashMap <String, cartitem> (); / *** 총* @return*/ public double getTotal () {// Total = 모든 항목의 하위 운동의 합계 bigdecimal total = new bigdecimal ( "0"); for (cartitem cartitem : map.values ()) {bigdecimal subtotal = new bigdecimal ( "" + cartitem.getSubtotal ()); Total = Total.add (하위 토탈); } return total.doublevalue (); }/*** 자동차에 항목 추가* @param cartitem*/public void add (cartitem cartitem) {if (map.containskey (cartitem.getbook (). getbid ())) {// 입력이 원래 자동차 catitem에 존재하는지 여부를 판단합니다. Entry_Cartitem.setCount (_cartitem.getCount () + cartitem.getCount ()); // 이전 항목의 수를 자체 번호로 설정 + 새 항목 맵. put (cartitem.getBook (). getbid (), _cartitem); } else {map.put (cartitem.getbook (). getbid (), cartitem); }} / *** 모든 항목을 지우십시오* / public void clear () {map.clear (); } / *** 지정된 항목 삭제* @param bid* / public void delete (문자열 입찰) {map.remove (bid); } / *** 모든 항목 가져옵니다* @return* / public Collection <Cartitem> getCartitems () {return map.values (); }} /*** 쇼핑 카트 엔트리 클래스**/public class cartitem {private book; // product private int count; // subtotal method* @return* 처리 된 바이너리 작동 오류 문제*/public double getSubtotal () {// 하위 메소드는 없지만 해당 구성원이 없습니다! bigdecimal d1 = new bigdecimal (book.getPrice () + ""); bigdecimal d2 = new bigdecimal (count + ""); d1. multiply (d2) .doublevalue (); } public book getBook () {반환 서적; } public void setbook (Book Book) {this.book = book; } public int getCount () {return count; } public void setCount (int count) {this.count = count; }}2. 쇼핑 카트 항목 추가
<%@ page language = "java"import = "java.util.*"pageencoding = "utf-8"%> <%@ taglib prefix = "c"uri = "http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %> < %@ taglib prefix = "fn"uri = "http://java.sun.com/jsp/jstl/functions" %> <! apptype html public "-//011 과도기 // en "> <html> <head> <title> 쇼핑 카트 목록 </title> <meta http-equiv ="pragma "content ="no-cache "> <meta http-equiv ="cache-control "content ="no-cache "> <meta http-equiv ="keyfres "0"> <mettp = "mettpiv" content = "keyword1, keyword2, keyword3"> <meta http-equiv = "description"content = "이것은 내 페이지입니다"> <meta http-equiv = "content-type"content = "text/html; charset = utf-8"> <!-<링크 rel = "stylesheet" "styl type = "text/css"> * {font-size : 11pt; } div {마진 : 20px; 테두리 : 단단한 2px 회색; 너비 : 150px; 높이 : 150px; 텍스트 정렬 : 센터; } li {마진 : 10px; } #buy {배경 : url (<c : url value = '/images/all.png'/>) no-Repeat; 디스플레이 : 인라인 블록; 배경 위치 : 0 -902px; 마진 왼쪽 : 30px; 높이 : 36px; 너비 : 146px; } #buy : hover {배경 : url (<c : url value = '/images/all.png'/>) no-Repeat; 디스플레이 : 인라인 블록; 배경 위치 : 0 -938px; 마진 왼쪽 : 30px; 높이 : 36px; 너비 : 146px; . value =/images/cart.png '/// "/> </c : 언제> <c : elide colspan ="7 "align ="right "style ="font-size : 900 "> <a href ="/cartllet 카트 </a> </td> </tr> <tr> <th> 그림 </th> <th> 책 제목 </th> <th> author </th> <th> 단원 가격 </th> <th> 수량 </th> <thtotal </th> <th> Operation </th> </tr> <c : feaceach items = "$}" "" "". <td> <td> <div> <img src = "<c : url value = '/$ {cartitem.book.image}'//"/> </div> </td> <td> $ {cartitem.book.bname} </td> <td> $ {cartitem.book. yuan </td> <td> $ {cartitem.count} </td> <td> $ {cartitem.subtotal} yuan </td> <td> <a href = "<c : url value = '/cartservlet? method = delete = $ {cartitem.book.book.book.book.bood}'/>"/> "/>"/> "/>"/> "/>"/> "/>"/> ">"/> "</>. </td> </td> </tr> </c : foreach> <td> <td colspan = "7"align = "right"style = "font-size : 15pt; 글꼴 중량 : 900 "> 총 : $ {sessionscope.cart.total} yuan </td> </tr> <tr> <td colspan ="7 "align ="right "style ="font-size : 15pt; 글꼴 중량 : 900 "> <a id ="구매 "href ="<c : url value = '/orderservlet? method = add'/> "> </a> </td> </tr> </table> </c : elid> </c : 선택> </body> </html> 공개 클래스 Cartservlet는 기본 서비스 { /** * 쇼핑 항목 추가 * @param request * @param 응답 * @return * @throws servletexception * @throws ioexception * /public string add (httpservletrequest 요청) servletexception, ioexception { / * 1. * 3. /** 양식은 입찰과 수량 만 통과합니다. Book Book = New Bookservice (). load (bid); int count = integer.parseint (request.getParameter ( "count")); cartitem cartitem = new cartitem (); Cartitem.setbook (Book); cartitem.setCount (count); /** 3. 자동차에 입구를 추가*/ cart.add (cartitem); "f : /jsps/cart/list.jsp"를 반환합니다. } / ** * 쇼핑 항목 * @param request * @param 응답 * @return * @throws servletexception * @throws ioexception * / public string clear (httpservletrequest 요청, httpservletreponse 응답) servletexception, ioexception { / ** * 1. (cart) request.getSession (). getAttribute ( "cart"); cart.clear (); "f : /jsps/cart/list.jsp"를 반환합니다. } / ** * 쇼핑 항목 삭제 * @param request * @param response * @return * @throws servletexception * @throws ioexception * / public string 삭제 (httpservletrequest request, httpservletrequest request, httpservletreponse 응답) servletexception, ioexception { / * 1. (cart) request.getSession (). getAttribute ( "cart"); 문자열 bid = request.getParameter ( "bid"); cart.delete (입찰); "f : /jsps/cart/list.jsp"를 반환합니다. }}3. 항목을 지우십시오
4. 쇼핑 카트 항목을 삭제합니다
5. 내 쇼핑 카트
Top.jsp : 내 쇼핑 카트에 링크가 있습니다
내 카트는 직접 /jsps/cart/list.jsp에 액세스하고 세션에 카트의 모든 항목을 표시합니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.