ResponseEntity

在ajax传输数据中取代map

controller界面

@RequestMapping("edit")
    public ResponseEntity<Void> edit(User user){
        try {
            ps.update(user);
            return ResponseEntity.status(HttpStatus.OK).build();
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

如果有泛型类型

@RequestMapping("list")
    public ResponseEntity<EasyUIResult> list(
            @RequestParam(value="page",defaultValue="1")Integer page,
            @RequestParam(value="rows",defaultValue="5")Integer rows
            ) {
        try {
            PageInfo<User> pageinfo=ps.list(page, rows);
            EasyUIResult easyUI=new EasyUIResult(pageinfo.getTotal(), pageinfo.getList());
            return ResponseEntity.status(HttpStatus.OK).body(easyUI);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

ajax代码,使用statusCode处理结果

$.ajax({
            "url":"user/edit",
            "type":"post",
            "data":$("#editcontent").serialize(),
            "statusCode":{
                200:function(){
                    $.messager.alert('提示','修改成功!');
                    $('#userUpdate').window('close');
                    $("#userList").datagrid("reload");
                },
                500:function(){
                    $.messager.alert('提示','服务器错误!');
                }
               
            }
            
        })
原文地址:https://www.cnblogs.com/psxfd4/p/11648432.html