3

我有下一个名为 的 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>

现在,我需要 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标签;简而言之,我不知道如何调整我的代码以使其正常工作。

有人帮我一把。

4

3 回答 3

3

你的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 - 复选框启用/禁用

使用 ajax 和 jsp/servlet 获取所有选中的复选框值?

于 2012-04-13T18:19:33.227 回答
2

在 JSP 中更改

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

或者

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

也将工作。您不需要转换为字符串值。

仅供参考:如果你打算做更多的事情。最好在参数中传递 b.get(i).getID()某种东西。传递序列可能会导致不正确的数据。

于 2012-04-13T10:09:42.767 回答
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);
于 2017-01-15T05:11:05.333 回答