2012-04-13 50 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);