从JSP页面获取复选框值

时间:2018-11-25 10:30:39

标签: java jsp java-ee checkbox checkboxlist

我试图将所有值从JSP页面中的选定复选框列表中获取到Java类。 那是我的JSP页面:

    <table border="1" cellpadding="5" cellspacing="1" style="width: 877px; ">
   <tr>
      <th>Code</th>
      <th>Name</th>
      <th>Price</th>
      <th>Select</th>
      <th>Edit</th>
      <th>Delete</th>
      <th>Show</th>
   </tr>
   <c:forEach items="${productList}" var="product" >
      <tr>
         <td>${product.code}</td>
         <td>${product.name}</td>
         <td>${product.price}</td>
         <td>
         <input type="checkbox" name="ProductItem"  value="${product.code}">
             <c:url value="ShowProductList" var="url">
             <c:param name="ProductItemP" value="${product.code}"/>
             </c:url> 
         </td>
         <td>
            <a href="editProduct?code=${product.code}">Edit</a>
         </td>
         <td>
            <a href="deleteProduct?code=${product.code}">Delete</a>
         </td>
         <td>
            <a href="ShowProduct?code=${product.code}">Show</a>
         </td>
      </tr>
   </c:forEach>
</table>


 <a href="${url}">Show Items</a>

如您所见,有一个表格,其中包含项目列表,每行都有一个复选框。在表格的末尾,有一个“显示项目”按钮,可以触发用户的请求。 那就是执行请求的servlet类:

    @WebServlet(urlPatterns = { "/ShowProductList" })
public class ShowProductList extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ShowProductList() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Connection conn = MyUtils.getStoredConnection(request);

             String[] paramValues = request.getParameterValues("ProductItemP");
             System.out.println(paramValues.length);
             response.sendRedirect(request.getContextPath() + "/productList");      
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

当用户选择两个或多个复选框并单击“显示项目”按钮时,单击该复选框后,我只能看到最后一个产品代码,而没有选中复选框时,不能看到所有代码。如果我尝试request.getParameterValues(“ ProductItem”);我有一个空值。我希望在JSP页面中没有代码(如果可能的话)。 有人可以帮我找到解决方案吗? 感谢您的耐心等候。

`

0 个答案:

没有答案