您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringMVC怎么接收參數(shù)各種場景”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringMVC怎么接收參數(shù)各種場景”吧!
此處的表單時 -使用JSON.stringify()函數(shù)將數(shù)組轉(zhuǎn)換成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()方法 提交表單內(nèi)容;
1、可以后臺使用 request.getParamter("對應(yīng)字段的name")獲取參數(shù);
2、也可以使用 Model mdel 的POJO接受。(name要一一對應(yīng)起來)
格式:var data = $("#formID").serialize();
功能:將表單內(nèi)容序列化成一個以&拼接的字符串,鍵值對的形式,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參數(shù)接收
@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接收。如果是復(fù)選框多個checkbox,則用數(shù)組String[] 接收。 String checkbox = request.getParameter("checkbox"); }
@RequestMapping(value="/addCustomer",method=RequestMethod.POST) @ResponseBody public LogisticsResult addCustomer(@Valid CustomerInfo customer,BindingResult result ){ 如果是復(fù)選框多個checkbox,則在pojo中 用與checkbox的name一樣的 數(shù)組接收。 如: String[] checkbox; }
接收List<String>集合參數(shù):
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[]集合參數(shù):
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', //設(shè)置請求頭信息 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[]數(shù)組,只需要把saveUsers的參數(shù)類型改為@RequestBody User[] userArray就行了。
接收List<Map<String,Object>>集合參數(shù):
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', //設(shè)置請求頭信息 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)集合參數(shù):
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', //設(shè)置請求頭信息 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怎么接收參數(shù)各種場景”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對SpringMVC怎么接收參數(shù)各種場景這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。