SpringMVC How to Receive Parameters for Various Scenarios
- 2021-12-05 06:25:36
- OfStack
Form submission
The form time here-uses the JSON. stringify () function to convert the array to json type and submit it to the background, which is processed using @ RequestBody User user
Page js
// Add Submit Button
$("#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) {
},
});
}
});
Back-end code
@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);
}
Form Submission 2
Submit the form content using the. serialize () method;
1. You can use request. getParamter ("name of corresponding field") to obtain parameters in the background;
2. POJO acceptance of Model mdel can also be used. (name should correspond to 11)
Format: var data = $("# formID"). serialize (); Function: Serializes the form content into 1 & Spliced string, key-value pair form, name1=val1 & name2=val2 & The space is replaced with% 20.Page JS
function sub(){
$.ajax({
type:"post",
url:"/restaurant/addEmployees.do",
data:$("#form").serialize(),
dataType :"json",
success:function(data){
if(!data.success){
}
});
}
Page html code:
<form action="" id="staff_form">
<div class="addInfor">
<input type="" name="phone" id="phone" value="" placeholder=" Please enter your mobile phone number "/>
<input type="" name="password" id="password" value="" placeholder=" Please enter your password "/>
<input type="" name="username" id="username" value="" placeholder=" Please enter a name "/>
<input name="checkbox" value="chief_store_member" type="checkbox" >
<label class="grey-font" > Multi-store management </label>
<input name="checkbox" value="branch_store_member" type="checkbox">
<label class="grey-font" > Single-store management </label>
</div>
<button type="button" class="mui-btn orange-btn" O nclick="sub();"> Confirm </button>
</form>
Background code receiving mode 1
Contains a single checkbox parameter receive
@RequestMapping("/addEmployees")
@ResponseBody
public Result<Integer> addEmployees(HttpServletRequest request) {
String phone = request.getParameter("phone");
String password = request.getParameter("password");
String username = request.getParameter("username");
Identity sheet checkbox Receive. If there are multiple check boxes, checkbox The array is used String[] Receive.
String checkbox = request.getParameter("checkbox");
}
Background code receiving mode 2
@RequestMapping(value="/addCustomer",method=RequestMethod.POST)
@ResponseBody
public LogisticsResult addCustomer(@Valid CustomerInfo customer,BindingResult result ){
If there are multiple check boxes, checkbox , then in the pojo Medium Use and checkbox Adj. name1 Like Array reception.
Such as: String[] checkbox;
}
Receive List < String > Collection parameters:
1. Page js code:
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 method:
@Controller
@RequestMapping("/catalog.do")
public class CatalogController {
@RequestMapping(params = "fn=deleteCatalogSchemes")
@ResponseBody
public AjaxJson deleteCatalogSchemes(@RequestParam("idList[]") List<String> idList,Boolean isBatch) {
…
}
}
Receive List < User > , User [] collection parameters:
1. User Entity Class:
public class User {
private String name;
private String pwd;
// Omission getter/setter
}
2. Page js code:
var userList = new Array();
userList.push({name: " Li 4",pwd: "123"});
userList.push({name: " Zhang 3",pwd: "332"});
$.ajax({
type: "POST",
url: "<%=path%>/catalog.do?fn=saveUsers",
data: JSON.stringify(userList),// Serializes an object into a JSON String
dataType:"json",
contentType : 'application/json;charset=utf-8', // Set request header information
success: function(data){
…
},
error: function(res){
…
}
});
3. Controller method:
@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);
}
0
If you want to receive an User [] array, just change the parameter type of saveUsers to @ RequestBody User [] userArray.
Receive List < Map < String,Object > > Collection parameters:
1. Page js code (User object is not needed):
@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);
}
1
2. Controller method:
@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);
}
2
Receive User (bean contains List) set parameters:
1. User Entity Class:
public class User {
private String name;
private String pwd;
private List<User> customers;// Customer groups belonging to users
// Omission getter/setter
}
2. Page js code:
@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);
}
4
3. Controller method:
@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);
}
5