jspからサーブレットへのパラメータの引き渡しをループで行う方法
jspで、シフト表のようなものを表示し、そのテキストボックスに入っている値をループでサーブレットに引き渡そうとしています。その際に、パラメータの設定をうまく行うことができません。
現在、ループで表現しようとしているのですが、引き渡すパラメータのnameもループしているため、ループの最後の値のみ引き渡しているような状態です。
どのようにnameを設定すれば引き渡しをすることができるのか、教えていただけたらとてもありがたいです。よろしくお願いします。
パラメータを渡すjsp(table部分のみ表記)
jsp
1<form action = "/Shifttable/RecordMember" method="post"> 2<table border=1> 3 <tr> 4 <th>ID</th> 5 <th>名前</th> 6 <th><%=month %>月<%=date %>日</th> 7 <th><%=month %>月<%=date+1%>日</th> 8 <th><%=month %>月<%=date+2%>日</th> 9 <th><%=month %>月<%=date+3%>日</th> 10 <th><%=month %>月<%=date+4%>日</th> 11 <th><%=month %>月<%=date+5%>日</th> 12 <th><%=month %>月<%=date+6%>日</th> 13 </tr> 14 15 <%for(Shift shift : shiftList) { //ループでシフトを代入、デフォルトの値で表示 %> 16 <tr align = "center"> 17 <td><%=shift.getId()%></td> 18 <td><%=shift.getName()%></td> 19 <td><input type="text" size="3" value= "<%=shift.getMon()%>" name="monday"></td> 20 <td><input type="text" size="3" value= "<%=shift.getTue()%>" name="tuesday"></td> 21 <td><input type="text" size="3" value= "<%=shift.getWen()%>" name="wednesday"></td> 22 <td><input type="text" size="3" value= "<%=shift.getThu()%>" name="thursday"></td> 23 <td><input type="text" size="3" value= "<%=shift.getFri()%>" name="friday"></td> 24 <td><input type="text" size="3" value= "<%=shift.getSat()%>" name="saturday"></td> 25 <td><input type="text" size="3" value= "<%=shift.getSun()%>" name="sunday"></td> 26 </tr> 27 <% } %> 28</table> 29<input type="submit" value="登録"> 30</form>
###パラメータを受け取るサーブレット(パラメータの取得からSQLの実行まで)
public class RecordMember extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //リクエストパラメータを取得 request.setCharacterEncoding("UTF-8"); String monday = request.getParameter("monday"); String tuesday = request.getParameter("tuesday"); String wednesday = request.getParameter("wednesday"); String thursday = request.getParameter("thursday"); String friday = request.getParameter("friday"); String saturday = request.getParameter("saturday"); String sunday = request.getParameter("sunday"); Connection conn = null; try { //JDBCドライバを読み込み Class.forName("org.h2.Driver"); //データベース接続 conn = DriverManager.getConnection("jdbc:h2:file:C:/data/example", "sa", ""); //UPDATE文を準備 Statement stmt = conn.createStatement(); stmt.executeUpdate("UPDATE SHIFT SET MON = '"+monday+"',TUE = '"+tuesday+"' ,WEN ='"+wednesday+"' ,THU ='"+thursday+"', FRI = '"+friday+"', SAT = '"+saturday+"', SUN = '"+sunday+"'");
追記部分
jsp
1 //この部分以外は変更していません 2 <%int loopcount = 0; %>//ループカウントの定義 3 <%for(Shift shift : shiftList) { //ループでシフトを代入、デフォルトの値で表示 %> 4 <tr align = "center"> 5 <td><%=shift.getId()%></td> 6 <td><%=shift.getName()%></td> 7 <td><input type="text" size="3" value= "<%=shift.getMon()%>" name="monday<%=loopcount%>"></td> 8 <td><input type="text" size="3" value= "<%=shift.getTue()%>" name="tuesday<%=loopcount%>"></td> 9 <td><input type="text" size="3" value= "<%=shift.getWen()%>" name="wednesday<%=loopcount%>"></td> 10 <td><input type="text" size="3" value= "<%=shift.getThu()%>" name="thursday<%=loopcount%>"></td> 11 <td><input type="text" size="3" value= "<%=shift.getFri()%>" name="friday<%=loopcount%>"></td> 12 <td><input type="text" size="3" value= "<%=shift.getSat()%>" name="saturday<%=loopcount%>"></td> 13 <td><input type="text" size="3" value= "<%=shift.getSun()%>" name="sunday<%=loopcount%>"></td> 14 </tr> 15 <%loopcount++; %>//ループカウントの加算 16 <% } %> 17 <input type="hidden" value="<%=loopcount%>" name="shiftListCount">
java
1 2//UPDATE文を準備(IDの指定方法を教えていただきたいです) 3 Statement stmt = conn.createStatement(); 4 for(int i = 0;i < (int)Integer.parseInt(request.getParameter("shiftListCount"));i++){ 5 monday = request.getParameter("monday"+i); 6 tuesday = request.getParameter("tuesday"+i); 7 wednesday = request.getParameter("wednesday"+i); 8 thursday = request.getParameter("thursday"+i); 9 friday = request.getParameter("friday"+i); 10 saturday = request.getParameter("saturday"+i); 11 sunday = request.getParameter("sunday"+i); 12 stmt.executeUpdate("UPDATE SHIFT SET MON = '"+monday+"',TUE = '"+tuesday+"' ,WEN ='"+wednesday+"' ,THU ='"+thursday+"', FRI = '"+friday+"', SAT = '"+saturday+"', SUN = '"+sunday+"' WHERE ID = "+i+1 ); 13 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/05 04:24
2018/12/05 04:26
2018/12/05 05:03
2018/12/05 05:05
2018/12/05 05:12
2018/12/05 05:13
2018/12/05 05:15
2018/12/05 05:16
2018/12/05 05:20
2018/12/05 05:25
2018/12/05 05:54
2018/12/05 05:59
2018/12/05 06:12
2018/12/05 14:52
2018/12/05 15:59