SpringMVC如何接收參數(shù)各種場景
表單提交
此處的表單時(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)容序列化成一個(gè)以&拼接的字符串,鍵值對的形式,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="請輸入手機(jī)號"/> <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();">確認(rèn)</button> </form>
后臺代碼接收方式一
含有單個(gè)的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ù)選框多個(gè)checkbox,則用數(shù)組String[] 接收。 String checkbox = request.getParameter("checkbox"); }
后臺代碼接收方式二
@RequestMapping(value="/addCustomer",method=RequestMethod.POST) @ResponseBody public LogisticsResult addCustomer(@Valid CustomerInfo customer,BindingResult result ){ 如果是復(fù)選框多個(gè)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實(shí)體類:
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實(shí)體類:
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(); … } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java避免UTF-8的csv文件打開中文出現(xiàn)亂碼的方法
這篇文章主要介紹了Java避免UTF-8的csv文件打開中文出現(xiàn)亂碼的方法,結(jié)合實(shí)例形式分析了java操作csv文件時(shí)使用utf-16le編碼與utf8編碼相關(guān)操作技巧,需要的朋友可以參考下2019-07-07淺析JavaWeb項(xiàng)目架構(gòu)之Redis分布式日志隊(duì)列
架構(gòu)、分布式、日志隊(duì)列,標(biāo)題自己都看著唬人,其實(shí)就是一個(gè)日志收集的功能,只不過中間加了一個(gè)Redis做消息隊(duì)列罷了。下面通過本文給大家分享JavaWeb項(xiàng)目架構(gòu)之Redis分布式日志隊(duì)列,感興趣的朋友一起看看吧2018-01-01fastjson對JSONObject中的指定字段重新賦值的實(shí)現(xiàn)
這篇文章主要介紹了fastjson對JSONObject中的指定字段重新賦值的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法
這篇文章主要介紹了Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot整合JSR303校驗(yàn)功能實(shí)現(xiàn),JSR303校驗(yàn)方法有統(tǒng)一校驗(yàn)的需求,統(tǒng)一校驗(yàn)實(shí)現(xiàn)以及分組校驗(yàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01淺談@Aspect@Order各個(gè)通知的執(zhí)行順序
這篇文章主要介紹了@Aspect@Order各個(gè)通知的執(zhí)行順序,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02