简体   繁体   中英

deteting row in DB using hibernate spring mvc

i am making CRUD app using hibernate spring MVC and JS .. and now i am working in deleteing a row in the DB .. here is my code my js

deleteRow : function() {
    $("input:checkbox:checked").each(bindContext(function(index, item) {
        var str = $(item).attr("id");
        str = str.substring(str.indexOf("_") + 1);

        $.ajax({
            url : '/Spring3HibernateApp1/delete',
            type : 'POST',
            data : {
                "id" : str,
            }
        });
        this.data.splice(str, 1);
        this.deleteTable();
        this.display();
    }, this));
},

my controller

@RequestMapping(value = "/delete")
public @ResponseBody void doPostDelete(@RequestBody String id,Employee employee){
    int idInt = Integer.parseInt(id);
    employee.setEmpId(idInt-1);
    employeeService.deleteEmployee(employee);
}

it enters to the controller and says jquery.min.js:4 POST http://localhost:8080/Spring3HibernateApp1/delete 500 (Internal Server Error)

any suggestions ??

HTTP 500 error mean, your controller worse

refactor your controller with this

@RequestMapping(value = "/delete/{id}" method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public void doPostDelete(@RequestBody Employee employee, @PathVariable("id") String id ) {
        int idInt = Integer.parseInt(id);
        employee.setEmpId(idInt - 1);
        employeeService.deleteEmployee(employee);
    }

and your JavaScript code like this

deleteRow : function() {
    $("input:checkbox:checked").each(bindContext(function(index, item) {
        var str = $(item).attr("id");
        str = str.substring(str.indexOf("_") + 1);

        $.ajax({
            url : '/Spring3HibernateApp1/delete/' + id, // id is your patvariable
            type : 'POST',
            data :  str // str must equal to employee json object
        });
        this.data.splice(str, 1);
        this.deleteTable();
        this.display();
    }, this));
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM