您好,登录后才能下订单哦!
这篇文章主要讲解了“SpringMVC怎么接收参数各种场景”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringMVC怎么接收参数各种场景”吧!
此处的表单时 -使用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(); … } }
感谢各位的阅读,以上就是“SpringMVC怎么接收参数各种场景”的内容了,经过本文的学习后,相信大家对SpringMVC怎么接收参数各种场景这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。