关于Spring MVC的数据绑定问题

最近在做第一个基于Spring MVC的项目,发现了一些在学习过程中没有注意到的问题,比如数据绑定,Json数据转换等等。。。

  • 数据绑定
    • 数据绑定最主要的问题就是复杂对象数组的绑定,当然,如果真在后台通过数组来绑定前台传递过来的Json字符串,会出很多问题,比如java.util.LinkedHashMap cannot be cast to com.xxx,要正确处理这个错误,真的会让人崩溃的。所以,我干脆在后台通过String来接收前台传过来的Json字符串,再对String进行处理,并且还有一点必须注意的是,不能设置contentType:”application/json”。具体看如下的代码注释:
//前台js代码
var mails = new Array();
            $.each(rows, function(i, n) {
            mails.push({"mail" : n.mail, "studentID" : n.studentID, "courseID" : courseID});
        });
        $.ajax({
            url : "course/send",
            type : "post",
            data : JSON.stringify(mails),
            dataType : "json",
        //  contentType : "application/json",   //当发送的json字符串转换前是数组,并且在服务器用数组接受,
        //一定要指定为"application/json",在传送到服务器的时候,因为之前已经设置的转换器,所以会自动将json字符串转化为数组;
            //而只是单一对象,就不能指定,会出错 ,因为在服务器也会用String接收。
            //所以在发送数组的时候,不要用$.post()方式而只能最好使用可以指定对应参数的$.ajax
            //当然,发送的Json字符串转换前是数组但是在服务器端以String类型接受,也不能指定"application/json",
            success : function(data) {
                $.messager.alert('提示', data.mes, 'info');
            },
            error : function(data) {
                $.messager.alert('提示', "发送失败", 'info');
            }
        }); 
//后台Controller
@RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseBody
    // 一个致命的低级错误,之前因为直接复制上面的sendCourse()方法,把@ResponseBody漏掉了,这样的话不管返回什么类型都会被系统检测为跳转到另外网页,但那个网页找不到,就会提示404 Not Found导致后面检查bug检查了好久。。。。。。
    //另外,对于下面的@RequestBody,当页面是通过ajax来发送数据,一定要指定@RequestBody
    public Map<String, String> send(@RequestBody String mails) throws Exception {

        Map<String, String> map = new HashMap<String, String>();
        //这里就是将Json字符串转化为List
        JSONArray json = JSONArray.fromObject(mails);
        List<SendModel> models = (List<SendModel>)   JSONArray.toCollection(json,SendModel.class);

        for(SendModel model : models){
            System.out.println(model);
        }
        //此处省略N行代码*******
        map.put("mes", "发送成功");
        return map;
    }
  • 需要指定content:”application/json”的情况
    正如前面所说的复杂对象数组的绑定如果在后台通过对应的对象数组来接受各种转换太麻烦了,所以在这里举例的是String数组的绑定,如果在后台通过String[]来接收就必须指定content:”application/json“(当然,在后台通过String先接受Json字符串是不用指定的),否则会出现415 Unsupported Media Type错误提示。Demo如下:
//前台js代码
        var test = new Array();
        test.push("aaa");
        test.push("bbb");
        test.push("ccc");
        alert(test);
        $.ajax({
            url : "course/send",
            type : "post",
            data : JSON.stringify(test),
            dataType : "json",
            contentType : "application/json",
            success : function(data) {
                $.messager.alert('提示', data.mes, 'info');
            },
            error : function(data) {
                $.messager.alert('提示', "发送失败", 'info');
            }
        });
//后台Controller
@RequestMapping(value = "/send", method = RequestMethod.POST)
@ResponseBody
    public Map<String, String> send(@RequestBody String[] test) throws Exception {
    for(String t : test){
        System.out.println(t);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值