美文网首页
Spring MVC 接收参数整理(2)-接收来自Ajax的请求

Spring MVC 接收参数整理(2)-接收来自Ajax的请求

作者: Ajinsir | 来源:发表于2017-11-25 12:54 被阅读0次

    1.前端Ajax请求方式

    (1)Ajax发送json对象

    这种方式是提交的Json对象,contentType为:application/x-www-form-urlencoded,后端可以@RequestParam

    var user = {
        username : "test",
        password:"test"
    };
    $.ajax({
        url:"/addUser.html",
        type:"POST",
        data:user,
        success:function(result) {
            console.log(result);
        }
    });
    
    (2)Ajax发送字符串(一般对于get请求,post一般不建议)

    这种方式contentType为:application/x-www-form-urlencoded,后端可以@RequestParam

    $.ajax({
        url:"/addUser.html",
        type:"GET",
        data:"username=test&sex=0",
        success:function(result) {
            console.log(result);
        }
    });
    

    这种方式参数一般较少,并且都是简单类型

    (3)Ajax发送json字符串

    发送json字符串,有三个地方需要注意,一是要指定type为json,二是要对数据JSON.stringify,三是要指定contenType为"contentType:'application/json;charset=UTF-8'

    var user = {
        username : "test",
        password:"test"
    };
    $.ajax({
        url:"/addUser.html",
        type:"POST",
        type:"json"
        data:JSON.stringify(user),
        contentType:"application/json;charset=UTF-8",
        success:function(result) {
            console.log(result);
        }
    });
    

    2.后端解析方式

    (1)第一种方式,直接依次获取参数,这种方式需要保持和前端传入参数名称一致
    @RequestMapping("/addUser.html")
    public void addUser(String username, String password){
        
    }
    
    (2)第二种方式,直接依次获取参数,使用@RequestParam,不要求名称一致
    @RequestMapping("/addUser.html")
    public void addUser(@RequestParam(value="username") String name, @RequestParam(value="username") String pwd){
    }
    
    (3)第三种方式,当参数比较多时,采用上述方式就比较麻烦,可以定义个pojo来处理
    public class User {
        
        private String username;
        
        private String password;
    }
    
    @RequestMapping("/addUser.html")
    public void addUser(User user){
    }
    
    (4)第四种方式,区别在于多一个注解@RequestBody
    public class User {
        
        private String username;
        
        private String password;
    }
    
    @RequestMapping("/addUser.html")
    public void addUser(@RequestBody User user){
    }
    
    (5)第五种方式,和第四种类似,使用@ModelAttribute
    public class User {
        
        private String username;
        
        private String password;
    }
    
    @RequestMapping("/addUser.html")
    public void addUser(@ModelAttribute User user){
    }
    

    3.使用方式分析

    总的原则:使用方式有三个维度:一是参数少且是简单类型?二是请求参数的contentType?三是是否支持不同名

    后端:

    • 同名简单类型,参数少且contentType为默认contentType为:application/x-www-form-urlencoded,后端直接在方法里通过变量名获取值
    • 不同名简单类型,参数少且contentType类型为默认值,使用@RequestParam注解
    • 复合类型,contentType默认,直接使用复合类型或加上@ModelAttribute注解
    • 复合类型,contentType不是默认,使用@RequestBody
    • 未添加注解的情况下,默认简单类型调用@RequesParam处理,复杂类型使用@ModelAttribute处理
    • @RequestBody的适用情况,有些时候需要发送前端需要发送包含数组的json数据,前端采用第三种方式

    前端:

    • 看参数多少和contentType来选择三种方式

    相关文章

      网友评论

          本文标题:Spring MVC 接收参数整理(2)-接收来自Ajax的请求

          本文链接:https://www.haomeiwen.com/subject/cpccbxtx.html