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)容序列化成一個以&拼接的字符串,鍵值對的形式,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>
后臺代碼接收方式一
含有單個的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實(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();
…
}
}
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA
本篇文章主要介紹了Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA,信息加密后數(shù)據(jù)更安全,需要的朋友可以參考下。2016-10-10
Java實(shí)現(xiàn)文件監(jiān)控器FileMonitor的實(shí)例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)文件監(jiān)控器FileMonitor的實(shí)例代碼,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Java枚舉_動力節(jié)點(diǎn)Java學(xué)院整理
enum 的全稱為 enumeration, 是 JDK 5 中引入的新特性,存放在 java.lang 包中。這篇文章給大家介紹Java枚舉相關(guān)知識,需要的的朋友參考下2017-04-04
詳解OpenCV For Java環(huán)境搭建與功能演示
這篇文章主要介紹了x詳解OpenCV For Java環(huán)境搭建與功能演示,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
詳解在java中進(jìn)行日期時(shí)間比較的4種方法
這篇文章主要介紹了詳解在java中進(jìn)行日期時(shí)間比較的4種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
關(guān)于mybatis傳入?yún)?shù)一直為null的問題
這篇文章主要介紹了關(guān)于mybatis傳入?yún)?shù)一直為null的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Springboot RestTemplate 簡單使用解析
這篇文章主要介紹了Springboot RestTemplate 簡單使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

