Spring MVC - delete record from JSP table

2019-09-04 18:49发布

问题:

I got table in JSP, which is table's mirror image from database (all records and columns are displayed), and next to each row I got button "delete" which deletes row from database by ID. But when I click "delete" button then nothing happens in database, it seems like selected row's ID is null, but at address bar selected ID is displayed. What am I doing wrong?

Controller:

@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();

}

Service:

public void deletePatient(String id) throws SQLException {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml");
    patientDAO = ctx.getBean("patientDAO", PatientDAOImpl.class);

    patientDAO.deletePatient(id);
}

JSP file:

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

Error(?):

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

回答1:

Use and GET rather than a POST and because on the url you are using the parameter selectedPatient rather than your expected id - change either/or



回答2:

Make your controller method get

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

}

change your delete link as follow

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

I dont see anyreason to make the request by post it is just one parameter, you dont need the form