オンラインショップをJAVAを使用しプログラムしています。
teratail内で同様のプログラムを作成している方がおり、参考にしながら作成を行ったのですが、画面推移時にエラーが発生し想定の動作にたどり着かないためご質問です。
デバッグを使用しlistの情報とgoodsNameが不一致のためエラーとなっているのは理解できるのですが、goodsNameは想定の値「油性ボールペン」だったので原因がつかめずにいます。
根本原因は何でしょうか。
RecordManager.java
@Repository @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) public final class RecordManager { /** 明細データ */ static List<Item> allList = new ArrayList<>(); /** 商品データ */ private static final List<Item> list = new ArrayList<Item>(); static { list.add(new Item("A00101", "油性ボールペン", 60, 0, 0)) /** * 更新した商品データ * 確認ボタンを押すとlistに書き込む。 */ private static Item newItem; /** * 引数で指定された商品名に一致する商品データを返却 * @param name 検索キーとなる商品名 * @return 検索結果の商品データ */ public static Item findItem(String goodsName) { int index = list.indexOf(new Item("", goodsName, 0,0,0)); return list.get(index); } /** * 商品データを返す。 * @return 商品データ */ public static List<Item> getNameist() { return list; } /** * 明細データを返す。 * @return 明細データ */ public static List<Item> getallList() { return allList; } /** * リストに商品情報を 1 件追加する */ public static void addToAllList(Item newItem) { Item item = new Item(newItem.getId(), newItem.getName(), newItem.getPrice(),newItem.getQuantity(),newItem.getSubtotal()); allList.add(item); } /** * リストに商品情報を 1 件削除する */ public static void removeToAllList(int number) { //Item item = new Item(newItem.getId(), newItem.getName(), newItem.getPrice(),newItem.getQuantity(),newItem.getSubtotal()); int i = 0; for (Item item : allList) { i += 1; if (i == number + 1) { allList.remove(item); return; } } } /** * リストに商品情報を クリアする */ public static void clearToAllList(Item newItem) { allList.clear(); } /** * newItemを商品データlistに書き込む。 */ public static void updateItem() { list.set(list.indexOf(newItem), newItem); } /** * newItemにデータを書き込む * @param ni セットする item */ public static void setNewItem(Item ni) { newItem = ni; } public static Item getNewItem() { return newItem; } /** * 全員の名前のリストを返す。 * @return 名前の配列 */ public static String[] makeNameList() { String[] nameList = new String[list.size()]; for (int i = 0; i < list.size(); i++) { nameList[i] = (list.get(i)).getName(); } return nameList; } /** * 名前で検索し、その商品のデータを返す。 * @param name 商品名 * @return 一商品データ */ public static Item selectItem(String name) { for(Item item:list) { if (item.getName().equals(name)) { return item; } } return null; } public static int calTotal(List<Item> list) { int total = 0; for(Item item: list) { total += item.getSubtotal(); } return total; } }
SalesSystemController.java
@Controller @RequestMapping(value = "/system") @SessionAttributes(types = {SalesForm.class}) public class SalesSystemController extends HttpServlet { private static final String ERRMSG = "点数には1以上100以下の整数を入力してください。"; private static final String ADDMSG = "明細に追加しました。"; // private static final String FIXMSG = "以下のように売上登録しました。"; private static final String DELETE = "選択された明細行を削除しました。"; private static final String ERRMSG2 = "明細行を選択して下さい。"; /** * 起動時および登録完了時に呼ばれる。初期画面を表示する。 * @param form フォームオブジェクト * @param model モデルオブジェクト * @return Viewとしてinit.jspを指定 */ @RequestMapping(value = "/start") public String init(SalesForm form, Model model) { model.addAttribute("nameList", RecordManager.makeNameList()); //model.addAttribute("nameList", RecordManager.makeNameList()); //form.setGoodsName(RecordManager.getFirstPersonName()); return "init"; } /** * 初期画面から呼ばれる。選択した商品の明細追加画面を表示する。 * @param form フォームオブジェクト * @param model モデルオブジェクト * @return Viewとしてadd.jspを指定 */ @RequestMapping(params = "add") public String add(SalesForm form,BindingResult result, Model model) { Item item = RecordManager.findItem(form.getGoodsName()); RecordManager.setNewItem(new Item(item.getId(), form.getGoodsName(), item.getPrice(), form.getQuantity(), item.getSubtotal())); Item newItem = RecordManager.getNewItem(); int sb = newItem.getPrice() * newItem.getQuantity(); newItem.setSubtotal(sb); model.addAttribute("nameList", RecordManager.makeNameList()); model.addAttribute("allList", RecordManager.getallList()); if (result.hasErrors() || 1 > form.getQuantity() || form.getQuantity() > 100) { model.addAttribute("total", RecordManager.calTotal(RecordManager.getallList())); model.addAttribute("message", ERRMSG); return "add"; } else { RecordManager.addToAllList(newItem); model.addAttribute("total", RecordManager.calTotal(RecordManager.getallList())); model.addAttribute("message2", ADDMSG); return "add"; } } /** * 細追加画面から呼ばれる。選択した商品を削除し明細追加画面を表示する。 * @param form フォームオブジェクト * @param model モデルオブジェクト * @return Viewとしてadd.jspを指定 */ @RequestMapping(params = "remove") public String remove(BindingResult result,HttpServletRequest request,SalesForm form, Model model) { String num = null; num = request.getParameter("radio"); int number = Integer.parseInt(num); RecordManager.removeToAllList(number); model.addAttribute("total", RecordManager.calTotal(RecordManager.getallList())); model.addAttribute("nameList", RecordManager.makeNameList()); model.addAttribute("allList", RecordManager.getallList()); if (result.hasErrors()) { model.addAttribute("message5", ERRMSG2); return "add"; }else { model.addAttribute("message4", DELETE); model.addAttribute("total", RecordManager.calTotal(RecordManager.getallList())); return "add"; } } }
init.jsp
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="/sales/resources/css/common.css" /> <title>売上システム(初期画面)</title> </head> <body> <form:form modelAttribute="salesForm" action="/sales/system" var="list"> <div class="header"> <span class="titleName">オンラインショップ</span> <% DateTimeFormatter format = DateTimeFormatter.ofPattern("MM月dd日(E)"); %> <span class="date"><%=format.format(LocalDate.now())%></span> </div> <div class="main"></div> <div>初期画面</div> 商品: <form:select path="goodsName" items="${nameList}" /> <div> 点数: <form:input path="quantity" items="${allList}" /> </div> <input type="submit" name="add" value="明細追加" /> </form:form> </body> </html>
Item.java
package jp.practice.sales; public class Item { /** 商品ID */ private String id; /** 商品名 */ private String name; /** 単価 */ private int price; /** 売上点数 */ private int quantity; /** 小景 */ private int subtotal; public Item(String id, String name, int price, int quantity, int subtotal) { this.id = id; this.name = name; this.price = price; this.quantity = quantity; this.subtotal = subtotal; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public int getSubtotal() { return subtotal; } /** * @param subtotal セットする subtotal */ public void setSubtotal(int subtotal) { this.subtotal = subtotal; } }
add.jsp
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="/sales/resources/css/common.css" /> <title>明細追加画面</title> </head> <body> <form:form modelAttribute="salesForm" action="/sales/system" var="list"> <div class="header"> <span class="titleName">オンラインショップ</span> <% DateTimeFormatter format = DateTimeFormatter.ofPattern("MM月dd日(E)"); %> <span class="date"><%=format.format(LocalDate.now())%></span> </div> <div class="main"></div> <div>明細追加画面</div> 商品: <form:select path="goodsName" items="${nameList}" /> <div> 点数: <form:input path="quantity" items="${allList}" /> </div> <div> <input type="submit" name="add" value="明細追加" /> </div> <div class="message"> <font color="#ff0000"> <c:out value="${message}" /></font> <font color="#1e90ff"> <c:out value="${message2}" /> </font> <font color="#1e90ff"> <c:out value="${message4}" /> </font><font color="#1e90ff"> <c:out value="${message5}" /> </font> </div> <table border="1"> <tr> <th>削除</th> <th>商品ID</th> <th>商品名</th> <th>単価</th> <th>点数</th> <th>小計</th> </tr> <c:forEach items="${allList}" var="item" varStatus="status"> <tr class="even"> <form:hidden path="quantity" /> <td><input type="radio" name="radio" value="${status.index}" /></td> <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.quantity}" /></td> <td><c:out value="${item.subtotal}" /></td> </tr> </c:forEach> </table> <div> 合計: <c:out value="${total}" /> 円 </div> <input type="submit" name="firm" value="確定" /> <input type="submit" name="remove" value="削除" /> </form:form> </body> </html>
まだ回答がついていません
会員登録して回答してみよう