javascript - 使用hibernate spring mvc检测数据库中的行

标签 javascript java spring hibernate spring-mvc

我正在使用 hibernate spring MVC 和 JS 制作 CRUD 应用程序..现在我正在删除数据库中的一行..这是我的代码 我的 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));
},

我的 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);
}

它进入 Controller 并显示 jquery.min.js:4 POST http://localhost:8080/Spring3HibernateApp1/delete 500(内部服务器错误)

有什么建议吗??

最佳答案

HTTP 500 错误意味着,你的 Controller 更糟

用这个重构你的 Controller

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

和你的 JavaScript 代码是这样的

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));
},

关于javascript - 使用hibernate spring mvc检测数据库中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36402483/

相关文章:

java - 如何在WildFly(耳朵中)中配置EJB WebServices

java - 从 C++ 到 Java 的转变

javascript - 无法使用 ChartJs 创建图例

javascript - Lint 双逗号数组

java - 从另一个 arrayList 中减去一个 arrayList

java - 刚接触 Maven 我的 Spring jar 在哪里

java - 不知道我在设置 Spring Boot REST 应用程序时做错了什么

java - 需要一种为 Spring Boot 生成 Java 类的方法

javascript - Vue js如何防止按钮连续点击两次

javascript - Angular 手动引导