という名前の次の jsp ファイルがあります。このファイルBookstore.jsp
では、データベースからのデータでテーブルを埋めました。
<%
ArrayList<Book> b = new ArrayList<Book>();
b = SqlSentencesList.showCatalog(); // this method returns an arrayList with all books
%>
<form method="get" action="ShoppingCarController">
<table border="2">
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Price</th>
<th>Select</th>
</tr>
<%for(int i=0; i<l.size();i++){%>
<tr>
<td> <%out.print(b.get(i).getIsbn());%> </td>
<td> <%out.print(b.get(i).getTitle());%> </td>
<td> <%out.print(b.get(i).getAuthor());%> </td>
<td> <%out.print(b.get(i).getPrice());%> </td>
<th> <input type="checkbox" name="checkboxGroup" value="<%Integer.toString(i);%>"/> </th>
</tr>
<% } %>
</table>
<input type="submit" value="Add to shopping car"/>
</form>
ここで、サーブレットに同じ本のデータ (ISBN、タイトル、著者、価格) が必要ですが、選択したものだけが必要です。
これは、ShoppingCarController サーブレットの doGet メソッドです。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Book> shoppingCar = new ArrayList<Book>();
String[] values = request.getParameterValues("checkboxGroup");
for(int i=0; i<values.length; i++) {
System.out.println(values[i]);
}
}
何が得られるかを確認するために印刷しようとしましたが、コンソールには何も表示されません。
私はこの同様のケースを見ていました:チェックボックスを使用して選択した行からデータを JSP からサーバーに渡す方法と、私の問題はvalue
属性にあると思いますが、その質問で使用されている構文がわかりません。for each
そして<c:out
タグ; 要するに、コードを機能させるためにコードを適応させる方法がわかりません。
誰か私に手を貸してください。