欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringMVC如何接收参数各种场景

程序员文章站 2022-03-28 22:06:18
目录表单提交表单提交二后台代码接收方式一后台代码接收方式二表单提交此处的表单时 -使用json.stringify()函数将数组转换成json类型提交后台,后台使用@requestbody user...

表单提交

此处的表单时 -使用json.stringify()函数将数组转换成json类型提交后台,后台使用@requestbody user user接受处理

页面js

//新增提交按钮
$("#buildsubmit").click(function() {
   var param = $(".form").serializejson();
   $.ajax({
    type: 'post',
    url: path + "/web/member/save.do",
    contenttype: "application/json",
    datatype: 'json',
    data: json.stringify(param),
    success: function(data) {
     
    },
   });
  }
 });

后端代码

@requestmapping(value = "/save", method = requestmethod.post)
public generalresponse save(@requestbody @valid memberinsertdetail member, bindingresult bindingresult)
   throws jsonprocessingexception {
  if (bindingresult.haserrors()) {
   throw new errparamexception();
  }
  boolean flag = false;
  flag = memberservice.save(member);
}

表单提交二

使用.serialize()方法 提交表单内容;

1、可以后台使用 request.getparamter("对应字段的name")获取参数;

2、也可以使用 model mdel 的pojo接受。(name要一一对应起来)

  • 格式:var data = $("#formid").serialize();
  • 功能:将表单内容序列化成一个以&拼接的字符串,键值对的形式,name1=val1&name2=val2&,空格以%20替换。

页面js

function sub(){
 $.ajax({
  type:"post",
  url:"/restaurant/addemployees.do",
  data:$("#form").serialize(),
  datatype :"json",
  success:function(data){
   if(!data.success){
  }
 }); 
}

页面html代码:

<form action="" id="staff_form">
<div class="addinfor">
<input type="" name="phone" id="phone" value="" placeholder="请输入手机号"/>
<input type="" name="password" id="password" value="" placeholder="请输入密码"/>
<input type="" name="username" id="username" value="" placeholder="请输入姓名"/>

<input name="checkbox" value="chief_store_member" type="checkbox" >
<label class="grey-font" >多店管理</label>
<input name="checkbox" value="branch_store_member" type="checkbox">
<label class="grey-font" >单店管理</label>
</div>
<button type="button" class="mui-btn orange-btn" οnclick="sub();">确认</button>
</form>

后台代码接收方式一

含有单个的checkbox参数接收

@requestmapping("/addemployees")
@responsebody
public result<integer> addemployees(httpservletrequest request) {
  string phone = request.getparameter("phone");
  string password = request.getparameter("password");
  string username = request.getparameter("username");
  身份单checkbox接收。如果是复选框多个checkbox,则用数组string[] 接收。
  string checkbox = request.getparameter("checkbox");
}

后台代码接收方式二

@requestmapping(value="/addcustomer",method=requestmethod.post)
@responsebody
public logisticsresult addcustomer(@valid customerinfo customer,bindingresult result ){
        如果是复选框多个checkbox,则在pojo中 用与checkbox的name一样的 数组接收。
        如: string[] checkbox;
}

接收list<string>集合参数:

1、页面js代码:

var idlist = new array();  
idlist.push(“1”);   
idlist.push(“2”);   
idlist.push(“3”);  
var isbatch = false;  
$.ajax({  
    type: "post",  
    url: "<%=path%>/catalog.do?fn=deletecatalogschemes",  
    datatype: 'json',  
    data: {"idlist":idlist,"isbatch":isbatch},  
    success: function(data){  
        …  
    },  
    error: function(res){  
        …  
    }  
});  

2、controller方法:

@controller  
@requestmapping("/catalog.do")  
public class catalogcontroller {    
    @requestmapping(params = "fn=deletecatalogschemes")  
    @responsebody  
    public ajaxjson deletecatalogschemes(@requestparam("idlist[]") list<string> idlist,boolean isbatch) { 
            …  
    }  
}  

接收list<user>、user[]集合参数:

1、user实体类:

public class user {  
        private string name;   
    private string pwd;  
    //省略getter/setter  
}

2、页面js代码:

var userlist = new array();  
userlist.push({name: "李四",pwd: "123"});   
userlist.push({name: "张三",pwd: "332"});   
$.ajax({  
    type: "post",  
    url: "<%=path%>/catalog.do?fn=saveusers",  
    data: json.stringify(userlist),//将对象序列化成json字符串  
    datatype:"json",  
    contenttype : 'application/json;charset=utf-8', //设置请求头信息  
    success: function(data){  
        …  
    },  
    error: function(res){  
        …  
    }  
});  

3、controller方法:

@controller  
@requestmapping("/catalog.do")  
public class catalogcontroller {    
    @requestmapping(params = "fn=saveusers")  
    @responsebody  
    public ajaxjson saveusers(@requestbody list<user> userlist) {  
        …  
    }  
}  

如果想要接收user[]数组,只需要把saveusers的参数类型改为@requestbody user[] userarray就行了。

接收list<map<string,object>>集合参数:

1、页面js代码(不需要user对象了):

var userlist = new array();  
userlist.push({name: "李四",pwd: "123"});   
userlist.push({name: "张三",pwd: "332"});   
$.ajax({  
    type: "post",  
    url: "<%=path%>/catalog.do?fn=saveusers",  
    data: json.stringify(userlist),//将对象序列化成json字符串  
    datatype:"json",  
    contenttype : 'application/json;charset=utf-8', //设置请求头信息  
    success: function(data){  
        …  
    },  
    error: function(res){  
        …  
    }  
});  

2、controller方法:

@controller  
@requestmapping("/catalog.do")  
public class catalogcontroller {  
  
    @requestmapping(params = "fn=saveusers")  
    @responsebody  
    public ajaxjson saveusers(@requestbody list<map<string,object>> listmap) {  
        …  
    }  
}  

接收user(bean里面包含list)集合参数:

1、user实体类:

public class user {  
    private string name;   
    private string pwd;  
    private list<user> customers;//属于用户的客户群  
    //省略getter/setter  
}  

2、页面js代码:

var customerarray = new array();  
customerarray.push({name: "李四",pwd: "123"});   
customerarray.push({name: "张三",pwd: "332"});   
var user = {};  
user.name = "李刚";  
user.pwd = "888";  
user. customers = customerarray;  
$.ajax({  
    type: "post",  
    url: "<%=path%>/catalog.do?fn=saveusers",  
    data: json.stringify(user),//将对象序列化成json字符串  
    datatype:"json",  
    contenttype : 'application/json;charset=utf-8', //设置请求头信息  
    success: function(data){  
        …  
    },  
    error: function(res){  
        …  
    }  
});  

3、controller方法:

@controller  
@requestmapping("/catalog.do")  
public class catalogcontroller {   
    @requestmapping(params = "fn=saveusers")  
    @responsebody  
    public ajaxjson saveusers(@requestbody user user) {  
        list<user> customers = user.getcustomers();  
        …  
    }  
}  

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。