从jsp到servlet的所选复选框值被发现为null

时间:2013-01-04 09:35:01

标签: jsp

我正在进行电子咨询项目。我想一次提供多个分支和学校的选择。我发现SelectedValues总是为null。请告诉我它有什么问题。

<table border="1" cellpadding="0" cellspacing="0" >
      <% for(i=0;i<branchSchools.length;i++){ %>
        <tr class=<%= (i%2==0)?"rowstyle":"altrowstyle" %> >
          <td>
            <input type="checkbox" name="SelectedBranchSchool"    id='SelectedBranchSchool<%= i %>' value="<%= i %>">
           </td>
            <td  >
              <%= branchSchools.item[i].branchCode %>
             </td>
             <td  >
               <%= branchSchools.item[i].school %>
              </td>
              <td align="right">
                <form name="frm5" action="addMultipleChoiceFill" method="post">
                  <% request.getSession().setAttribute("BranchSchools",branchSchools);
                     request.getSession().setAttribute("FilledChoices",filledChoices);
                  %>
                 <input type="submit" name="addmult"  value="AddMultiple"/>
               </form>
          </td>
   ![enter image description here][1]

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    BranchSchools branchSchools=null;
    FilledChoices filledChoices=null;
    branchSchools=(BranchSchools) request.getSession().getAttribute("BranchSchools");
    request.getSession().removeAttribute("BranchSchools");
    filledChoices=(FilledChoices)request.getSession().getAttribute("FilledChoices");
    request.getSession().removeAttribute("FilledChoices");
    String[] SelectedValues = request.getParameterValues("SelectedBranchSchool");
    for(int i=0; i<SelectedValues.length; i++){
        int k=Integer.valueOf(SelectedValues[i]);
        filledChoices.addFilledChoice(branchSchools.item[k]);
        branchSchools.removeBranchSchool(k);
    }
    request.setAttribute("BranchSchools",branchSchools);
    request.setAttribute("FilledChoices",filledChoices);
    request.setAttribute("Change", "yes");
    RequestDispatcher rd=getServletContext().getRequestDispatcher("/choiceFill.jsp?");
    rd.forward(request,response);

1 个答案:

答案 0 :(得分:1)

您的复选框不在表单中,因此在提交表单时不会提交:

<input type="checkbox" name="SelectedBranchSchool" ...>
...
<form name="frm5" action="addMultipleChoiceFill" method="post">
   ...
   <input type="submit" name="addmult"  value="AddMultiple"/>
</form>

另外,请尊重Java命名约定,并阅读有关MVC,JSTL和最佳实践的内容。