Bookstore.jsp
と呼ばれる次のjspファイルがあります。このファイルでは、データベースのデータでテーブルを埋めました。サーブレットとJSPを使用してhtmlテーブルから選択した行を取得
<%
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]);
}
}
私が取得していますかを確認するためにそれを印刷しようとしたが、何もコンソールに表示しません。
私は、この同様のケースを見ていた:How to pass data from selected rows using checkboxes from JSP to the serverと私は私の問題は、value
属性であるが、私はその質問に使用される構文を知らない、そのfor each
と<c:out
タグを理解していないと思います。一言で言えば、私は自分のコードをどのようにしてそれを動作させるのかを知らない。
誰かが私にそれを手渡す。
書籍には、タイトル、著者、価格、ISBN番号があります。私はテーブルを埋めるためにそれを使用します、私はISBN(それはIDである)だけを渡すことができません。 –
サーブレットのコードスニペットに基づいて回答しました。 –