In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how SpringMVC receives parameters in various scenarios". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how SpringMVC receives parameters in various scenarios".
Form submission
When the form here-use the JSON.stringify () function to convert the array to json type and submit it to the background, and the background uses @ RequestBody User user to accept processing
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 two
Use the .serialize () method to submit the form content
1. You can use request.getParamter ("name of the corresponding field") to obtain parameters at backend.
2. You can also accept it using Model mdel's POJO. (name should be matched one by one)
Format: var data = $("# formID") .serialize ()
Function: serialize the contents of the form into a & concatenated string, in the form of key-value pairs, and replace name1=val1&name2=val2&, spaces 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:
Multi-store management single-store management confirmation background code reception mode 1
Contains a single checkbox parameter receiver
@ RequestMapping ("/ addEmployees") @ ResponseBodypublic Result addEmployees (HttpServletRequest request) {String phone = request.getParameter ("phone"); String password = request.getParameter ("password"); String username = request.getParameter ("username"); identity document checkbox receive. If it is a check box with more than one checkbox, it is received with the array String []. String checkbox = request.getParameter ("checkbox");} background code receiving method 2 @ RequestMapping (value= "/ addCustomer", method=RequestMethod.POST) @ ResponseBodypublic LogisticsResult addCustomer (@ Valid CustomerInfo customer,BindingResult result) {if it is a check box with more than one checkbox, it is received with the same array as checkbox's name in pojo. For example, String [] checkbox;}
Receive List 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: "/ 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 idList,Boolean isBatch) {… }}
Receive List, User [] collection parameters:
1. User entity class:
Public class User {private String name; private String pwd; / / omit getter/setter}
2. Page js code:
Var userList = new Array (); userList.push ({name: "Li Si", pwd: "123"}); userList.push ({name:" Zhang San ", pwd:" 332 "}); $.ajax ({type:" POST ", url:" / catalog.do?fn=saveUsers ", data: JSON.stringify (userList), / / serialize the object into the JSON string dataType:" json ", contentType: 'application/json Charset=utf-8', / / set request header information success: function (data) {… }, error: function (res) {… })
3. Controller method:
@ Controller @ RequestMapping ("/ catalog.do") public class CatalogController {@ RequestMapping (params = "fn=saveUsers") @ ResponseBody public AjaxJson saveUsers (@ RequestBody List userList) {… }}
If you want to receive the User [] array, you just need to change the parameter type of saveUsers to @ RequestBody User [] userArray.
Receive List collection parameters:
1. Page js code (no need for User object):
Var userList = new Array (); userList.push ({name: "Li Si", pwd: "123"}); userList.push ({name:" Zhang San ", pwd:" 332 "}); $.ajax ({type:" POST ", url:" / catalog.do?fn=saveUsers ", data: JSON.stringify (userList), / / serialize the object into the JSON string dataType:" json ", contentType: 'application/json Charset=utf-8', / / set request header information success: function (data) {… }, error: function (res) {… })
2. Controller method:
@ Controller @ RequestMapping ("/ catalog.do") public class CatalogController {@ RequestMapping (params = "fn=saveUsers") @ ResponseBody public AjaxJson saveUsers (@ RequestBody List listMap) {… }}
Receive User (bean contains List) collection parameters:
1. User entity class:
Public class User {private String name; private String pwd; private List customers;// belongs to the user's customer base / / omit getter/setter}
2. Page js code:
Var customerArray = new Array (); customerArray.push ({name: "Li Si", pwd: "123"}); customerArray.push ({name: "Zhang San", pwd: "332"}); var user = {}; user.name = "Li Gang"; user.pwd = "888"; user. Customers = customerArray; $.ajax ({type: "POST", url: "/ catalog.do?fn=saveUsers", data: JSON.stringify (user), / / serialize the object into the JSON string dataType: "json", contentType: 'application/json;charset=utf-8', / / set request header information success: function (data) {... }, error: function (res) {… })
3. Controller method:
@ Controller @ RequestMapping ("/ catalog.do") public class CatalogController {@ RequestMapping (params = "fn=saveUsers") @ ResponseBody public AjaxJson saveUsers (@ RequestBody User user) {List customers = user.getCustomers ();... }} Thank you for your reading. the above is the content of "how SpringMVC receives parameters in various scenarios". After the study of this article, I believe you have a deeper understanding of how SpringMVC receives parameters in various scenarios, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.