最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC如何接收參數(shù)各種場(chǎng)景

 更新時(shí)間:2021年11月01日 09:38:37   作者:沖動(dòng)的仔bb  
這篇文章主要介紹了SpringMVC如何接收參數(shù)各種場(chǎng)景,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

表單提交

此處的表單時(shí) -使用JSON.stringify()函數(shù)將數(shù)組轉(zhuǎn)換成json類型提交后臺(tái),后臺(tái)使用@RequestBody User user接受處理

頁(yè)面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、可以后臺(tái)使用 request.getParamter("對(duì)應(yīng)字段的name")獲取參數(shù);

2、也可以使用 Model mdel 的POJO接受。(name要一一對(duì)應(yīng)起來(lái))

  • 格式:var data = $("#formID").serialize();
  • 功能:將表單內(nèi)容序列化成一個(gè)以&拼接的字符串,鍵值對(duì)的形式,name1=val1&name2=val2&,空格以%20替換。

頁(yè)面JS

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

頁(yè)面html代碼:

<form action="" id="staff_form">
<div class="addInfor">
<input type="" name="phone" id="phone" value="" placeholder="請(qǐng)輸入手機(jī)號(hào)"/>
<input type="" name="password" id="password" value="" placeholder="請(qǐng)輸入密碼"/>
<input type="" name="username" id="username" value="" placeholder="請(qǐng)輸入姓名"/>

<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>

后臺(tái)代碼接收方式一

含有單個(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");
}

后臺(tái)代碼接收方式二

@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、頁(yè)面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、頁(yè)面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),//將對(duì)象序列化成JSON字符串  
    dataType:"json",  
    contentType : 'application/json;charset=utf-8', //設(shè)置請(qǐng)求頭信息  
    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、頁(yè)面js代碼(不需要User對(duì)象了):

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),//將對(duì)象序列化成JSON字符串  
    dataType:"json",  
    contentType : 'application/json;charset=utf-8', //設(shè)置請(qǐng)求頭信息  
    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、頁(yè)面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),//將對(duì)象序列化成JSON字符串  
    dataType:"json",  
    contentType : 'application/json;charset=utf-8', //設(shè)置請(qǐng)求頭信息  
    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)文章

最新評(píng)論

金沙县| 惠安县| 秦安县| 宜阳县| 博乐市| 白玉县| 井冈山市| 西充县| 游戏| 永康市| 普格县| 遵义县| 宁南县| 腾冲县| 湾仔区| 成都市| 汶川县| 荣成市| 隆化县| 巴青县| 三门峡市| 富民县| 涿州市| 禄丰县| 潮安县| 泰安市| 伊通| 平顶山市| 新乡县| 葫芦岛市| 金沙县| 阿拉善右旗| 钟山县| 睢宁县| 都江堰市| 闻喜县| 阳西县| 盐池县| 克山县| 黔西县| 比如县|