0

I am working in an e-counselling project. I want to provide selection of multiple branch and schools at a time.I found that SelectedValues is always null.Please tell me what is wrong with it.

<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);
4

1 回答 1

1

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

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

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

于 2013-01-04T09:45:08.117 回答