0

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
dante
  • 393
  • 1
  • 15
  • 31
  • 1
    try debugging it, does the code get called correctly, is the `id` value set correctly? any null pointer obejcts? any errors? Are you just missing a transaction/commit? – Scary Wombat Feb 17 '16 at 01:38
  • For test I put random ID from database in parameter, like that: clinicService.deletePatient(15) and row with ID 15 has been deleted from database after click button, so connection between service and dao is fine, so the problem is with jsp or parameters in 'checkOut' method in controller, errors?...only SQLException, check post – dante Feb 17 '16 at 02:20
  • so it looks like your id is not being passed correctly which is probably because you are using `POST`, hence your queryString variables will not be used. – Scary Wombat Feb 17 '16 at 02:29
  • @ScaryWombat: I tried do it like answer below, but its the same result...is here any proper solution? Its weird, because in my previous app it worked without any issues – dante Feb 17 '16 at 02:50
  • 1
    Because on the url you are using the parameter `selectedPatient` – Scary Wombat Feb 17 '16 at 02:51
  • Omg, I'm so stupid.... THANK YOU, it works now :) – dante Feb 17 '16 at 02:57

2 Answers2

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

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
-1

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

Alaa Abuzaghleh
  • 1,023
  • 6
  • 11
  • I did it like that, but still nothing happened, it seems like selected ID from address bar dont want to inject in method's parameter. For test I put random ID from database in parameter, like that: clinicService.deletePatient(15) and row with ID 15 has been deleted from database after click button, so connection between service and dao is fine – dante Feb 17 '16 at 02:11
  • change your @RequestParam(value = "id", required = false) to @RequestParam(value = "selectedPatient", required = false) it must work – Alaa Abuzaghleh Feb 17 '16 at 04:27