我在JSP中获得了表,该表是数据库中表的镜像(显示了所有记录和列),并且在每一行的旁边都有“删除”按钮,该按钮按ID从数据库中删除了行。但是,当我单击“删除”按钮时,数据库中什么也没有发生,似乎所选行的ID为空,但是在地址栏中显示了所选ID。我究竟做错了什么?
控制器:
@RequestMapping(value="/checkout.html", method = RequestMethod.POST) public ModelAndView checkOut(Model model, @RequestParam(value = "id", required = false) String id) throws SQLException{ setAppContext(); clinicService.deletePatient(id); List<Patient> patients = clinicService.getAllpatients(); model.addAttribute("patients", patients); ModelAndView checkout = new ModelAndView("CheckOut"); return checkout; }
道:
public void deletePatient(String id) throws SQLException { String query = "delete FROM virtualclinic.patient WHERE idpatient=?"; Connection con = null; PreparedStatement ps = null; con = dataSource.getConnection(); ps = con.prepareStatement(query); ps.setString(1, id); int out = ps.executeUpdate(); }
服务:
public void deletePatient(String id) throws SQLException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml"); patientDAO = ctx.getBean("patientDAO", PatientDAOImpl.class); patientDAO.deletePatient(id); }
JSP文件:
<c:forEach items="${patients}" var="patient"> <tr style="font-size: 10"> <td>${patient.id}</td> <td>${patient.name}</td> <td>${patient.lastName}</td> <td>${patient.gender}</td> <td>${patient.age}</td> <td>${patient.phoneNumber}</td> <td>${patient.address}</td> <td>${patient.disease}</td> <td>${patient.condition}</td> <td>${patient.roomType}</td> <td>${patient.roomNumber}</td> <td>${patient.date}</td> <td><form action="/VirtualClinic/checkout.html?selectedPatient=${patient.id}" method="post"><input type="submit" value="Delete"/></form></td> </tr> </c:forEach>
错误(?):
INFO: Mapped "{[/checkout.html],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView org.damian.controller.CheckOutController.infoPatient(org.springframework.ui.M odel) throws java.sql.SQLException lut 17, 2016 3:16:57 AM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMa pping register INFO: Mapped "{[/checkoutPatient.html],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView org.damian.controller.CheckOutController.checkOut(org.springframework.ui.Model,java.lang.String) throws java.sql.SQLException
使用和GET而不是POST,因为在url上您使用的是参数selectedPatient而不是预期的参数id-更改
selectedPatient
id