###質問内容
まず初めてこちらに質問するので何か不手際が御座いましたら教えて頂けると有難いです。
よろしくお願いします。
-Javaを勉強していて今ショッピングカートのシステムを構築しています。
機能的にはDBに登録しているユーザー情報、商品情報を取得してログインまた商品一覧画面に表示。
商品一覧画面でselectで個数選択し「カートへ入れる」でカート内の商品確認画面へsubmit。
カート内の商品確認画面で「購入」ボタンを押すと購入完了画面を表示し商品一覧へ戻ると予めDBに登録していた在庫数から購入数を引いて表示するといった感じです。
今現在、DBから商品を取得し商品一覧画面で個数を選択しカート画面を表示するところまでは実装出来ています。最後のDBに登録している在庫数から購入数を引くという操作をどうコーディングするのかが分かりません。
下記に必要と思われるコードを載せますので皆様のお知恵をお貸しください。
初心者故にぐちゃぐちゃなコーディングだと思います。
すみませんが宜しくお願いいたします。
###Javaコントローラー
package com.jissyu.shopping2.controller; ※import文は省略します。 @Controller public class ItemController { private static final int NUM_PER_PAGE = 5; @Autowired private LocationDao locationDao; @Autowired private ItemDao itemDao; @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); format.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true)); } @RequestMapping(value = { "/", "/listItem" }) public String list(@RequestParam(name = "p", defaultValue = "1") Integer page, Model model) throws Exception { List<Item> itemList = itemDao.findByPage(page, NUM_PER_PAGE); model.addAttribute("itemList", itemList); int totalPages = itemDao.totalPages(NUM_PER_PAGE); model.addAttribute("totalPages", totalPages); model.addAttribute("currentPage", page); return "listItem"; } @RequestMapping(value = "/addItem", method = RequestMethod.GET) public String addGet(Model model) throws Exception { List<Location> locationList = locationDao.findAll(); model.addAttribute("locationList", locationList); Item item = new Item(); model.addAttribute("item", item); return "addItem"; } @RequestMapping(value = "/addItem", method = RequestMethod.POST) public String addPost(@Valid Item item, Errors errors, Model model) throws Exception { List<Location> locationList = locationDao.findAll(); model.addAttribute("locationList", locationList); if (!errors.hasErrors()) { itemDao.insert(item); return "addItemDone"; } else { return "addItem"; } } @RequestMapping(value = "/buyItem", method = RequestMethod.POST) public String buyItem(@RequestParam Integer id, @RequestParam Integer count, Model model, HttpSession session) throws Exception { @SuppressWarnings("unchecked") List<Cart> cartList = (List<Cart>) session.getAttribute("cartList"); if (cartList == null) { cartList = new ArrayList<>(); } boolean itemFound = false; for (Cart cart : cartList) { Integer cartId = cart.getId(); if (id == cartId) { cart.setCount(cart.getCount() + count); itemFound = true; break; } } if (!itemFound) { Cart cartA = new Cart(); cartA.setId(id); Item itemdb = itemDao.findById(id); cartA.setCount(count); cartA.setName(itemdb.getName()); cartA.setPrice(itemdb.getPrice()); cartList.add(cartA); } session.setAttribute("cartList", cartList); return "buyItem"; } //ここにDBの在庫数から購入数を引く計算をするコードを書こうとしています } }
###jsp
---listItem.jsp--- <%@ page 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="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="<spring:url value="/css/bootstrap.min.css" />" rel="stylesheet" /> <link href="<spring:url value="/css/style.css" />" rel="stylesheet" /> <title>ECサイト システム</title> </head> <body> <div class="container"> <c:import url="parts/header.jsp" /> <h1>商品一覧</h1> <div class="row"> <div class="col-md-12"> <c:import url="parts/pagination.jsp" /> <table class="table table-bordered"> <tr> <th>商品コード</th> <th>商品名</th> <th>価格</th> <th>発送地</th> <th>在庫数</th> <th>商品登録日</th> <th>購入</th> </tr> <c:forEach items="${itemList}" var="item"> <tr> <td><c:out value="${item.itemId}" /></td> <td><c:out value="${item.name}" /></td> <td><c:out value="${item.price}" /></td> <td><c:out value="${item.location.name}" /></td> <td><c:out value="${item.stock}" /></td> <td><c:out value="${item.updated}" /></td> <td><c:if test="${item.stock == '0'}" var="flg" /> <c:if test="${flg}" >在庫切れ</c:if> <c:if test="${!flg}"> <form method="POST" action="buyItem"> <input type="hidden" name="id" value="${item.id}" /> <select name="count"> <c:forEach var="stocks" begin="0" end="${item.stock}"> <option value="${stocks}">${stocks}</option> </c:forEach> <c:if test="${item.stock!='0'}" var="check"> <input type="submit" value="カートへ入れる"> </c:if> </select> </form> </c:if> </tr> </c:forEach> </table> <a href="<spring:url value="/addItem" />" class="btn btn-primary">商品の追加</a> </div> </div> </div> <script src="<spring:url value="/js/jquery-3.2.1.min.js" />"></script> <script src="<spring:url value="/js/bootstrap.min.js" />"></script> </body> </html> ---buyItem.jsp--- <%@ page 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="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="<spring:url value="/css/bootstrap.min.css" />" rel="stylesheet" /> <link href="<spring:url value="/css/style.css" />" rel="stylesheet" /> <title>ECサイト システム</title> </head> <body> <div class="container"> <c:import url="parts/header.jsp" /> <h1>ショッピングカート</h1> <div class="row"> <p>次の商品を購入しますか?</p> <table class="table table-bordered"> <tr> <th>商品ID</th> <th>商品名</th> <th>価格</th> <th>購入数</th> </tr> <c:forEach items="${cartList}" var="item"> <tr> <td><c:out value="${item.id}" /></td> <td><c:out value="${item.name}" /></td> <td><c:out value="${item.price}" /></td> <td><c:out value="${item.count}" /></td> </tr> </c:forEach> </table> </div> <a href="<spring:url value="/listItem" />" class="btn btn-default">買い物を続ける</a> <br> <br> <form method="POST" action="buyItemDone"> <input type="submit" class="btn btn-primary" value="購入する" /> </form> </div> </body> </html>
###補足情報(言語/FW/ツール等のバージョンなど)
・Java SE8
・Spring Framework(MVC) 4.3.11
・Hibernate ORM
・MySQL WorkBench6.3CE
・TomCat8
以上です。何か足りない情報等あると思いますので随時補足していきます。
宜しくお願いいたします
回答1件
あなたの回答
tips
プレビュー