2012-04-13 49 views
2

我有下一個名爲Bookstore.jsp的jsp文件,其中我使用數據庫中的數據填充表。使用Servlets和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> 

現在,我需要在Servlet同一本書的數據(ISBN,書名,作者和價格),但只是從選定的人。

這是從ShoppingCarController我的servlet的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看起來應該事端像這樣(使用您已發佈的servlet代碼)

首先編輯你的servlet,包括:

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

在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%>"/> 

也會起作用。您不需要輸入字符串值。

供參考:如果你打算做更多的事情。在參數中更好的通過 b.get(i).getID()種事物。傳遞序列可能會導致數據不正確。

+0

一本書有一個標題,一個作者,一個價格和一個ISBN號碼。我用它來填滿表格,我無法通過ISBN(這將是ID)。 – 2012-04-13 13:09:46

+0

我已根據您的servlet代碼段進行了回答。 – 2012-04-13 14:15:18

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);