2012-04-13 19 views
2

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タグを理解していないと思います。一言で言えば、私は自分のコードをどのようにしてそれを動作させるのかを知らない。

誰かが私にそれを手渡す。

答えて

2

あなたのJSPが(あなたが投稿したサーブレットのコードを使用して)このよう

をのsomethinを見なければならない最初の編集サーブレットと含まれます:あなたが持っているでしょう、あなたのJSPで

ArrayList<Book> shoppingCar = new ArrayList<Book>(); 
request.setAttribute("b", shoppingCar);//accsessed as ${b} in jsp 

を: -

 <form action="yourserlet" method="POST"> 
       <table> 
        <thead> 
         <tr> 
          <td width="10%">ISBN</td> 
          <td width="30%">TITLE</td> 
          <td width="30%">AUTHOR</td> 
          <td width="20%">SELECT</td> 
         </tr> 
        </thead> 

        <tbody> 

     <c:forEach items="${b}" var="book"> 
       <tr>  
        <td align="left"><input type="text" name="isbn<c:out value="${book.isbn}"/>" disabled="true"/></td>      
        <td align="left"><input type="text" name="title<c:out value="${book.title}"/>" disabled="true"/></td> 
        <td align="left"><input type="text" name="author<c:out value="${book.author}"/>" disabled="true"/></td> 
        <td align="left"><input type="text" name="price<c:out value="${book.price}"/>" disabled="true"/></td> 
        <td align="center"> 
         <input type="checkbox" name="checkboxgroup" 
          value="c:out value="${book.tostring()}"/>"/> 
        </td> 
        </tr> 
      </c:forEach> 
     </tbody> 
       </table> 
      </form> 

jqueryを使用してチェックボックスをオンにしてフィールドを有効または無効にする必要がありますが、デフォルトで無効にしています。

もチェック:

jQuery - checkbox enable/disable

Getting all selected checkboxes values using ajax and jsp/servlets?

2
JSPの変更で

<input type="checkbox" name="checkboxGroup" value="<%=Integer.toString(i)%>"/> 

OR

<input type="checkbox" name="checkboxGroup" value="<%=i%>"/> 

も動作します。文字列値にキャストする必要はありません。

FYI:もっと計画を立てようとしている場合。より良いパラメータを渡す b.get(i).getID()物事を渡す。シーケンスを渡すと不正なデータになることがあります。

+0

書籍には、タイトル、著者、価格、ISBN番号があります。私はテーブルを埋めるためにそれを使用します、私はISBN(それはIDである)だけを渡すことができません。 –

+0

サーブレットのコードスニペットに基づいて回答しました。 –

1

あなたのJSPコード..

<form method="POST" action="promoteSelected"> 
<table class="table table-striped table-bordered"> 
<thead> 
<tr> 
<th>*</th> 
<th>AdmNo</th> 
<th>Firstname</th> 
<th>Lastname</th> 
<th>Check</th> 
</tr> 
</thead> 
<tbody> 
<% 
if(studentList !=null){ 
    int scount = 1; 
    for(Student stu : studentList){ 
    %> 
<tr> 
<td><%=scount%></td> 
<td><%=stu.getAdmno()%></td> 
<td><%=stu.getFirstname()%></td> 
<td><%=stu.getLastname()%></td> 
<td> 
<div class="checkbox">  
<input type="hidden" name="studentId[]" value="<%=stu.getUuid()%>">  
<label><input type="checkbox" name="studentCheck[]">Check</label> 
</div>    
</td> 
</tr> 
<% 
scount++; 
} } 
%> 

</tbody> 
</table> 
<div class="form-actions"> 
<button type="submit" class="btn btn-primary"> 
<input type="hidden" name="schooluuid" value="<%=accountuuid%>"> 
     Promote 
</button> 
</div> 
</form> 

Sevletコード..

String[] studentCheck = {}; 
String[] studentId = {}; 

studentCheck = request.getParameterValues("studentCheck[]"); 
studentId = request.getParameterValues("studentId[]"); 
String schooluuid = StringUtils.trimToEmpty(request.getParameter("schooluuid")); 

for(String str : studentCheck){ 
    System.out.println("studentCheck " + str); 
} 

for(String str : studentId){ 
    System.out.println("studentId " + str); 
} 
System.out.println("schooluuid " + schooluuid); 
関連する問題