0309gunner
0309gunner

Reputation: 31

how to retrieve the data from the check boxes in JSP into controller class in springframework

here's the JSP page snippet:

<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 />

and this is a snippet of my controller class

@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;
}

I would like to retrieve the values of the checked boxes from the JSP page in the controller class.

Upvotes: 1

Views: 125

Answers (1)

Marcin Bukowiecki
Marcin Bukowiecki

Reputation: 410

Try this one:

<% 
  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
}

Upvotes: 1

Related Questions