【问题标题】:deteting row in DB using hibernate spring mvc使用hibernate spring mvc在DB中检测行
【发布时间】:2016-07-23 23:37:51
【问题描述】:

我正在使用 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));
},

我的控制器

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

它进入控制器并显示 jquery.min.js:4 POST http://localhost:8080/Spring3HibernateApp1/delete 500 (Internal Server Error)

有什么建议吗??

【问题讨论】:

  • 你能提供堆栈跟踪吗?
  • 没有看到任何日志,我认为您正在托盘转换来自String id 的非字符串数字表示形式的数字...您可以打印id 值吗?
  • 点击删除按钮时..我试图捕捉员工的 id 并将其发送到控制器并从数据库中删除它
  • @Hector 有什么想法吗?
  • 您能否验证您正在发送id 变量中的数字?或提供堆栈跟踪?

标签: javascript java spring hibernate spring-mvc


【解决方案1】:

HTTP 500 错误的意思是,你的控制器更糟

用这个重构你的控制器

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 2013-08-21
    • 1970-01-01
    • 2012-12-25
    • 2016-01-03
    • 2016-11-07
    • 2014-11-20
    • 2014-07-11
    相关资源
    最近更新 更多