前提・実現したいこと
SpringMVCフレームワークを使用しています
画面は初期画面(init.jsp)と明細追加画面(add.jsp)があり、初期画面から明細追加画面に遷移する際の上記塩分気の方法がわかりません。
希望の動作としては、
1.初期画面に「商品」プルダウン、「点数」テキストボックス、[明細追加]ボタンを設置。
2.「商品」プルダウンで商品を選び、「点数」テキストボックスに購入したい数を記入し[明細追加]ボタンを押下する。
3.異常がなければ明細追加画面に遷移する。異常が発生した場合初期画面へ戻る。
3の条件としては、正常処理が「点数」テキストボックスに(1以上の数字)を入力したとき。異常処理が「点数」テキストボックスに(1以上の数字以外)を入力したときもしくは未入力のとき。
という感じで条件分岐させたいです。
発生している問題・エラーメッセージ
現在は入力に関係なく明細追加ボタンを押下すると、初期画面から明細追加画面へ遷移します。
###該当のソースード
SalesSystemController.java
java
1package jp.practice.sales; 2 3//import org.springframework.beans.factory.annotation.Autowired; 4//import org.springframework.context.MessageSource; 5import org.springframework.stereotype.Controller; 6import org.springframework.ui.Model; 7import org.springframework.web.bind.annotation.RequestMapping; 8 9/** 10 * Handles requests for the application home page. 11 */ 12@Controller 13@RequestMapping(value = "/system") 14public class SalesSystemController { 15 16 // @Autowired 17 // private MassageSource massageSource; 18 // private String message; 19 20 private static final String INIT = "init"; 21 private static final String ADD = "add"; 22 private static final String FIX = "fix"; 23 24 @RequestMapping(value = "/start") 25 public String init(SalesForm form, Model model) { 26 model.addAttribute("allList", RecordManager.getItemList()); 27 model.addAttribute("nameList", RecordManager.makeNameList()); 28 return INIT; 29 } 30 @RequestMapping(params = "add") 31 public String add(SalesForm form, Model model) { 32 model.addAttribute("allList", RecordManager.selectProduct(form.getName())); 33 model.addAttribute("nameList", RecordManager.makeNameList()); 34 return ADD; 35 } 36 @RequestMapping(params = "firm") 37 public String firm(SalesForm form, Model model) { 38 return FIX; 39 } 40}
RecordManager.java
java
1package jp.practice.sales; 2 3import java.util.ArrayList; 4import java.util.List; 5//商品一覧 6/** 7 * 商品データ全体を保持するクラス 8 */ 9public final class RecordManager { 10 11 /** 商品データ */ 12 private static List<Item> list = new ArrayList<>(); 13 private static List<Item> selectList = new ArrayList<>(); 14 private static Item item; 15 16 static { 17 list.add(new Item("A00101", "油性ボールペン", 60, 0, 0)); 18 list.add(new Item("A00201", "極細ボールペン", 120, 0, 0)); 19 list.add(new Item("A00301", "蛍光ペン6色セット", 420, 0, 0)); 20 list.add(new Item("A00401", "シャープペンシル", 100, 0, 0)); 21 list.add(new Item("A00501", "鉛筆H(1ダース)", 400, 0, 0)); 22 list.add(new Item("B00101", "無線綴ノートA4", 100, 0, 0)); 23 list.add(new Item("B00201", "リングノートA4", 120, 0, 0)); 24 list.add(new Item("B00301", "領収書", 350, 0, 0)); 25 list.add(new Item("C00101", "はさみ(青)", 128, 0, 0)); 26 list.add(new Item("C00201", "ステープラー", 338, 0, 0)); 27 list.add(new Item("C00301", "2穴パンチ", 128, 0, 0)); 28 list.add(new Item("C00401", "ゼムクリップ", 98, 0, 0)); 29 list.add(new Item("C00501", "消しゴム", 58, 0, 0)); 30 } 31 public static List<Item> selectProduct(String name) { 32 Item item = new Item("",name,0,0,0); 33 selectList.add(list.get(list.indexOf(item))); 34 return selectList; 35 } 36 public static String[] makeNameList() { 37 String[] nameList = new String[list.size()]; 38 for (int i = 0; i < list.size(); i++) { 39 nameList[i] = (list.get(i)).getName(); 40 } 41 return nameList; 42 } 43 public static List<Item> getItemList() { 44 return list; 45 } 46 public static void setItemList() { 47 list.set(list.indexOf(item), item); 48 } 49}
Item.java
java
1package jp.practice.sales; 2 3/** 4 * 売上明細1件分のデータを保持するクラス 5 */ 6public class Item { 7 /* (非 Javadoc) 8 * @see java.lang.Object#hashCode() 9 */ 10 @Override 11 public int hashCode() { 12 final int prime = 31; 13 int result = 1; 14 result = prime * result + quantity; 15 return result; 16 } 17 /* (非 Javadoc) 18 * @see java.lang.Object#equals(java.lang.Object) 19 */ 20 @Override 21 public boolean equals(Object obj) { 22 if (this == obj) { 23 return true; 24 } 25 if (obj == null) { 26 return false; 27 } 28 if (!(obj instanceof Item)) { 29 return false; 30 } 31 Item other = (Item) obj; 32 if (quantity != other.quantity) { 33 return false; 34 } 35 return true; 36 } 37 /** 38 * @return id 39 */ 40 public String getId() { 41 return id; 42 } 43 /** 44 * @param id セットする id 45 */ 46 public void setId(String id) { 47 this.id = id; 48 } 49 /** 50 * @return name 51 */ 52 public String getName() { 53 return name; 54 } 55 /** 56 * @param name セットする name 57 */ 58 public void setName(String name) { 59 this.name = name; 60 } 61 /** 62 * @return price 63 */ 64 public int getPrice() { 65 return price; 66 } 67 /** 68 * @param price セットする price 69 */ 70 public void setPrice(int price) { 71 this.price = price; 72 } 73 /** 74 * @return quantity 75 */ 76 public int getQuantity() { 77 return quantity; 78 } 79 /** 80 * @param quantity セットする quantity 81 */ 82 public void setQuantity(int quantity) { 83 this.quantity = quantity; 84 } 85 /** 86 * @return subtotal 87 */ 88 public int getSubtotal() { 89 return subtotal; 90 } 91 /** 92 * @param subtotal セットする subtotal 93 */ 94 public void setSubtotal(int subtotal) { 95 this.subtotal = subtotal; 96 } 97 /** 商品id */ 98 private String id; 99 /** 商品名 */ 100 private String name; 101 /** 単価 */ 102 private int price; 103 /** 個数 */ 104 private int quantity; 105 /** 小計 */ 106 private int subtotal; 107 /** 108 * コンストラクターです。 109 * @param id 商品id 110 * @param name 商品名 111 * @param price 単価 112 * @param quantity 個数 113 * @param subtotal 小計 114 */ 115 public Item(String id, String name, int price, int quantity, int subtotal) { 116 this.id = id; 117 this.name = name; 118 this.price = price; 119 this.quantity = quantity; 120 this.subtotal = subtotal; 121 } 122}
SalesForm.java
java
1 /** 商品id */ 2 private String id; 3 /** 商品名 */ 4 private String name; 5 /** 単価 */ 6 private int price; 7 /** 個数 */ 8 private int quantity; 9 /** 小計 */ 10 private int subtotal; 11 12 以下、上記のコンストラクタとgetterとsetterがあります
init.jsp
<%@ page session="false" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.time.LocalDate"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!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"> <div class="header"> <span class="titleName">直太郎オンラインショップ</span> <div class="date"><%=LocalDate.now()%></div> </div> <div class="main"> <div> 商品:<form:select path="name" items="${nameList}" /><br/> </div> <div> <span class="itemQuantity">点数:</span> <form:input path="quantity" size="20" /> </div> </div> <div> <input type="submit" name="add" value="明細追加" /> </div> <div class="message"> <c:out value="${message}" /> </div> </form:form> </body>
add.jsp
<%@ page session="false" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.time.LocalDate"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!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"> <div class="header"> <span class="titleName">直太郎オンラインショップ</span> <div class="date"><%=LocalDate.now()%></div> </div> <div class="main"> <div> 商品: <form:select name="id" path="name" items="${nameList}" /> <br /> </div> <div> <span class="itemQuantity">点数:</span> <form:input path="quantity" size="5" value="1" /> </div> <div class="add"> <div> <input type="submit" name="add" value="明細追加" /> </div> </div> </div> </form:form> <form:form modelAttribute="salesForm" action="/sales/system"> <div class="main2"> <div class="message"> <c:out value="${message}" /> </div> <div> <span class="details">売上明細</span> <table class="product"> <tr> <th>削除</th> <th>商品ID</th> <th>商品名</th> <th>単価</th> <th>点数</th> <th>小計</th> </tr> <c:forEach items="${allList}" var="allList" varStatus="status"> <tr> <th><form:radiobutton path="name" label="" value="${allList.name}" /></th> <th>${allList.id}</th> <th>${allList.name}</th> <th>${allList.price}</th> <th>${allList.quantity}</th> <th>${allList.subtotal}</th> </tr> </c:forEach> </table> </div> <div> 合計:<fmt:formatNumber value="${total}" groupingUsed="true" />円 </div> </div> <div class="footer"> <input type="submit" name="remove" value="削除" /> <input type="submit" name="firm" value="確定" /> </div> </form:form> </body> </html>
補足情報(FW/ツールのバージョンなど)
Eclipse Java EE IDE for Web Developers.
Version: Mars.2 Release (4.5.2)
ローカルホストTomcat8
回答1件
あなたの回答
tips
プレビュー