• SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解


    request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用:

    http://blog.csdn.net/walkerjong/article/details/7946109  (关于更多参数绑定常用的注解)

    @PathVariable

               $.ajax({
                    url: ctx + '/management/cart/delete/'+id,
                    async: false,
                    cache: false,
                    type: "POST",
                    success: function (data) {
                        
                    },
                    error: function (xhr) {
                        
                    }
                });
    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public int delete(@PathVariable int id){
        return 0;
    }
    //若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称
    public int delete(@PathVariable("id") int rsId){
        return 0;
    }
     

    @RequestParam

    A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;

    B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;

    C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;

    $.ajax({
                    url: ctx + '/management/cart/delete?id='+id,
                    async: false,
                    cache: false,
                    type: "POST",
                    success: function (data) {
                        
                    },
                    error: function (xhr) {
                        
                    }
                });
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public int delete(@RequestParam("id") int id){
        return 0;
    }

    @RequestBody

    该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,而是application/json, application/xml等;

    基于ajax的方法请求,将contentType设置为application/json

    var cartId = 1;
    $.ajax({
                    url: ctx + '/management/cart/delete',
                    async: false,
                    cache: false,
                    type: "POST",
                    contentType: "application/json",
                    data: cartId,
                    success: function (data) {
                        
                    },
                    error: function (xhr) {
                        
                    }
                });
    
    @ResponseBody
        @RequestMapping(value = "/delete", method = RequestMethod.POST)
        public int delete(@RequestBody Integer id) {
            return 0;
        }

    @RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象变成字符串。同时ajax请求的时候也要指定contentType:"application/json" 这样就可以轻易的将一个对象或者List传到Java端,使用@RequestBody即可绑定对象或者List.

    var  params = [1,2,3];
    $.ajax({
                    url: ctx + '/management/cart/delete',
                    async: false,
                    cache: false,
                    type: "POST",
                    contentType: "application/json",
                    data: JSON.stringify(params),
                    success: function (data) {
    
                    },
                    error: function (xhr) {
                        
                    }
                });
    @ResponseBody
        @RequestMapping(value = "/deletes", method = RequestMethod.POST)
        public int deletes(@RequestBody List<Integer> ids) {
            return 0;
        }
    @ResponseBody
        @RequestMapping(value = "/create", method = RequestMethod.POST)
        public int create(@RequestBody ExtTaskAssignmentCfg extTaskAssignmentCfg) {
            UserInfo userInfo = contextService.getContextUserInfo();
            extTaskAssignmentCfg.setInsman(userInfo.getUsername());
            extTaskAssignmentCfg.setInsdate(new Date());
            return this.extTaskAssignmentCfgService.insert(extTaskAssignmentCfg);
        }
    
    var jsonObject = {};
                jsonObject.id = assignmentCfgId;
                jsonObject.activitiId = activityId;
                jsonObject.configurationDetail = configurationDetail;
                var jsonStr = JSON.stringify(jsonObject);
                $.ajax({
                    url: url,
                    async: false,
                    cache: false,
                    type: "POST",
                    contentType:"application/json",
                    data: jsonStr,
                    success: function (data) {
                    },
                    error: function (xhr) {
                        alert("保存受理人分配配置失败")
                    }
                })
    @RequestMapping(value = "saveUser", method = {RequestMethod.POST }}) 
        @ResponseBody  
        public void saveUser(@RequestBody List<User> users) { 
             userService.batchSave(users); 
        }
    
    
    var saveDataAry=[];  
            var data1={"userName":"test","address":"gz"};  
            var data2={"userName":"ququ","address":"gr"};  
            saveDataAry.push(data1);  
            saveDataAry.push(data2);         
            $.ajax({ 
                type:"POST", 
                url:"user/saveUser", 
                dataType:"json",      
                contentType:"application/json",               
                data:JSON.stringify(saveDataAry), 
                success:function(data){ 
                                           
                } 
             }); 
  • 相关阅读:
    ElasticSearch已经配置好ik分词和mmseg分词(转)
    Java:Cookie实现记住用户名、密码
    (转)10大H5前端框架
    msysGit在GitHub代码托管
    mongodb 语句和SQL语句对应(SQL to Aggregation Mapping Chart)
    centos 7 安装redis
    mac下idea卡顿问题解决
    在linux系统中,使用tomcat的shutdown.sh脚本停止应用,但是进程还在的解决办法
    centOS 7.4 安装配置jdk1.8
    CentOS6 在线安装PostgreSQL10
  • 原文地址:https://www.cnblogs.com/samwang88/p/6598919.html
Copyright © 2020-2023  润新知