Spring MVC-从JSP表中删除记录 [英] Spring MVC - delete record from JSP table

查看:314
本文介绍了Spring MVC-从JSP表中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSP中获得了表,该表是数据库中表的镜像(显示所有记录和列),并且在每一行旁边都有删除"按钮,该按钮通过ID从数据库中删除行.但是,当我单击删除"按钮时,数据库中没有任何反应,似乎所选行的ID为空,但是在地址栏中显示了所选ID.我在做什么错了?

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?

控制器:

@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

推荐答案

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

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

这篇关于Spring MVC-从JSP表中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆