我在项目中使用servlet。
这是代码:
public PrintWriter showBorrowed(HttpServletResponse response, String name, List<Book> books) throws IOException {
PrintWriter out = response.getWriter();
startPartial(out, name);
out.println("<form action=\"srvReturnBooks\" method=\"post\">");
for (int i = 0; i < books.size(); i++) {
out.println("<tr>");
Book b = books.get(i);
out.println("<td>" + b.getTitle() + "</td>");
out.println("<td>" + b.getIsbn() + "</td>");
out.println("<td>" + b.getAuthorName() + "</td>");
out.println("<td>" + b.getPublishDate() + "</td>");
out.println("<td><input type=\"checkbox\" data-studentId=\"" + b.getid() + "\" name=\"checkbox\"></td>");
out.println("</tr>");
}
out.println("<input type=\"submit\" name=\"btnReturn\" value=\"Return Book\"/>");
out.println("</form>");
endPartial(out);
return out;
}
单击按钮提交后,将触发此功能:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
htmlHelper = new HTMLHelper();
String[] checked = request.getParameterValues("checkbox");
htmlHelper.showMessage(response, "Returned - Thank you!");
}
通过此行:
String[] checked = request.getParameterValues("checkbox");
我获得了所有选中的复选框,但是我还需要获取每个复选框的data-studentId属性值。
知道我如何为每个复选框获取ata-studentId属性吗?