Spring MVC - 从JSP表中删除记录

时间:2016-02-17 01:34:39

标签: java mysql spring jsp spring-mvc

我在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;

}

DAO:

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

2 个答案:

答案 0 :(得分:1)

使用和GET而不是POST,因为您在网址上使用参数selectedPatient而不是预期的id - 更改/或

答案 1 :(得分:-1)

让你的控制器方法获得

@RequestMapping(value="/checkout.html", method = RequestMethod.GET)
public ModelAndView checkOut(Model model, @RequestParam(value = "selectedPatient", 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;

}

更改您的删除链接,如下所示

 <td><a href="/VirtualClinic/checkout.html?selectedPatient=${patient.id}">delete</a></td>

我没有看到通过帖子发出请求的任何原因它只是一个参数,你不需要表格