如何将JSP中的复选框中的数据检索到springframework中的控制器类中

时间:2015-04-18 14:28:25

标签: spring jsp controller

这是JSP页面代码段:

<table>
  <tr>
    <th class="checkbox"></th>
    <th>ID no.</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Department</th>
    <th>Experience</th>
  </tr>
  <% 
    List<Faculty> faculty = (List<Faculty>)request.getAttribute("facultylist");
    for (Faculty record : faculty) {
    %>
  <tr id="id_1">
    <td class="checkbox"><input type="checkbox" name="checkbox" value=<%=record.getId()%> /></td>
    <td><%=record.getId()%></td>
    <td><%=record.getName()%></td>
    <td>lastname</td>
    <td><%=record.getDept()%></td>
    <td><%=record.getExp()%></td>
  </tr>
  <%    } %>                                    
</table>
</div>                          
</div>
</div>
<p class="last" align="right">
  <input type="submit" value="submit" class="novisible" />
  <a href="/InvigilatorRandomizer/logs" class="button form_submit"><span>Submit</span></a>
  <br />

这是我的控制器类的片段

@RequestMapping("/logs")
public ModelAndView helloWorld3() {
    ModelAndView modelandview = new ModelAndView("logs");
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-dispatcher-servlet.xml");
    FacultyJDBCTemplate facultyJDBCTemplate = (FacultyJDBCTemplate) context.getBean("facultyJDBCTemplate");

    //retreive list of faculty that are selected from submit.jsp and create the new list of faculty(selected)
    List < Faculty > faculty = facultyJDBCTemplate.listFaculty();
    RoomJDBCTemplate roomJDBCTemplate = (RoomJDBCTemplate) context.getBean("roomJDBCTemplate");
    List < Room > room = roomJDBCTemplate.listRoom();

    modelandview.addObject("facultylist", faculty);
    modelandview.addObject("roomlist", room);
    ((ConfigurableApplicationContext) context).close();

    return modelandview;
}

我想从控制器类的JSP页面中检索已选中框的值。

1 个答案:

答案 0 :(得分:1)

试试这个:

<% 
  List<Faculty> faculty = (List<Faculty>)request.getAttribute("facultylist");  %>
  <input type="hidden" name="maxId" value="<%= faculty.get(faculty.size()-1).getId()%>" />
    <%
    for (Faculty record : faculty) {
    %>
      <tr id="id_1">
      <td class="checkbox"><input type="checkbox" name="checkbox<%=record.getId()%>" value=<%=record.getId()%> /></td>
          <td><%=record.getId()%></td>
          <td><%=record.getName()%></td>
          <td>lastname</td>
          <td><%=record.getDept()%></td>
          <td><%=record.getExp()%></td>
      </tr>
    <%  } %> 

public ModelAndView getParams(HttpServletRequest request){
    List<String> params = new ArrayList<String>();
    String maxId = request.getParameter("maxId");
    Integer lastId = new Integer(maxId); 

      for(int i=0; i<lastId ;i++){
         String param = request.getParameter("checkbox"+i);
         if(param != null) params.add(param);
      }
    ....rest stuff
}