Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use controller to pass boolean form values

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use controller to transmit boolean formal value". The content of 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 to use controller to transmit boolean formal value".

Controller passes the boolean form value @ GetMapping ("/ check-cart") public List checkCart (@ RequestParam (value = "requirePrice", required = false) boolean requirePrice) {return service.checkCart (requirePrice);}

Controller passes a value of type boolean. The parameter is not required.

We can pass on three types.

Http:// Port: ip/cart/check-cart

Http:// Port: ip/cart/check-cart?requirePrice=true

Http:// Port: ip/cart/check-cart?requirePrice=false

All three are fine.

Others such as:

Http:// Port: ip/cart/check-cart?requirePrice=

Http:// Port: ip/cart/check-cart?requirePrice=111

Will report an error.

The controller layer receives various parameters and files

When building a system, the front end and background always need to be docked. In the springmvc architecture, this kind of docking usually occurs in the Controller layer. Method parameter binding first supports all the basic types of Java (including byte, short, int, long, float, double, char, string, boolean), as well as the corresponding encapsulation of advanced classes (including: StringBuilder, StringBuffer), as well as various JavaBean types defined by ourselves. There are many ways to accept, but you should also reflect the idea of object-oriented when accepting data.

Simple input

Take the user login as an example, pass in the data in the foreground, and do an incoming example.

The data passed in from the foreground is in the form of username=10&password=10

/ / pass parameter $.ajax in simple form ({type: "POST", url: "http://localhost:8080/test/requesetParamGet", contentType:" application/x-www-form-urlencoded ", data:" username=10&password=10 ", dataType:" json " Success: function (result) {if (result.code = = 0) {console.log (result)} else {})

Under such circumstances, how can it be accepted in the background? The following methods can be used.

@ ResponseBody @ RequestMapping ("/ simple") public R list (String name,String age) {System.out.println ("name:" + name+ ", age:" + age); return R.ok ();}

Of course, it can also be accepted in the form of HttpServeletRequest

@ ResponseBody @ RequestMapping ("/ simple") public R requestGet (HttpServletRequest request) {System.out.println ("reqname:" + request.getParameter ("name") + ", reqage:" + request.getParameter ("age")); return R.ok ();} is passed in as a json object

Or take the user login as an example, pass in the data in the foreground, and do an incoming example.

/ / get class You need to use the object form var s = {name:'liMin', age:'10'} $.ajax ({type: "POST", url: "http://localhost:8080/test/classGet", contentType:" application/json ", data:JSON.stringify (s)) DataType: "json", success: function (result) {if (result.code = = 0) {console.log (result)} else {})

In this case, the controller layer recommends acceptance in the form of objects.

ResponseBody @ RequestMapping ("/ classGet") public R classGet (@ RequestBody TestEntity testEntity) {System.out.println ("className:" + testEntity.getName () + "classAge:" + testEntity.getAge ()); return R.ok ();}

When passing parameters, you need to add the annotation @ RequsetBody to receive incoming objects whose contentType is application/json. If the contentType header is application/x-www-form-urlencoded when it is passed, it is recommended to use another annotation accept @ RequestParam to accept it.

@ RequestMapping ("/ requesetParamGet") public R addUser6 (@ RequestParam ("username") String username,@RequestParam ("password") String password) {System.out.println ("username is:" + username); System.out.println ("password is:" + password); return R.ok ();}

If you don't write anything, you won't get the object, and the object you receive will be NULL.

Of course, you can not accept it as an object, you can convert the passed json object into a json string, and then parse it with various tools. Of course, you have to add @ RequestBody or @ RequestParam.

@ ResponseBody @ RequestMapping ("/ stringGet") public R stringGet (@ RequestBody String string) {System.out.println ("String:" + string); return R.ok ();} File transfer

In a project, file upload is different from object upload.

Function savePic () {var formData = new FormData ($("# uploadPic") [0]); var ajaxUrl = "http://localhost:8080/test/fileUpload"; / / alert (ajaxUrl); / / $('# uploadPic'). Serialize () cannot serialize the binary file. Here the formData upload / / requires browser support: Chrome 7 +, Firefox 4 +, IE 10 +, Opera 12 +, Safari 5 +. $.ajax ({type: "POST", / / dataType: "text", url: ajaxUrl, data: formData, / / async: false,// cache: false, contentType: false,// upload file processData: false,// serialization processing. Default is true. Upload files need to be changed to false success: function (data) {alert (data) }, error: function (data) {alert ("error:" + data.responseText);}}); return false;} function jiance () {var formData = new FormData () FormData.append ()} submit jiance

Example of accepting parameters in the background:

@ RequestMapping ("/ fileUpload") @ ResponseBody public R upload (MultipartFile multipartFile) {String filePath= "; if (! multipartFile.isEmpty ()) {System.out.println (multipartFile.getOriginalFilename ());} return R.ok (). Put (" filePath ", filePath);} @ RequestMapping (" / fileUpload2 ") @ ResponseBody public R upload2 (@ RequestParam (" multipartFile ") MultipartFile multipartFile) {String filePath=" If (! multipartFile.isEmpty ()) {System.out.println (multipartFile.getOriginalFilename ());} return R.ok () .put ("filePath", filePath);} @ RequestMapping ("/ fileUpload3") @ ResponseBody public R upload3 (@ RequestBody MultipartFile multipartFile) {String filePath= "; if (! multipartFile.isEmpty ()) {System.out.println (multipartFile.getOriginalFilename ()) } return R.ok () .put ("filePath", filePath);}

It should be noted here that the file name must be consistent with the parameter name, and the name of the file file in this project must be multipartFile.

Thank you for your reading, the above is the content of "how to use controller to transmit the formal value of boolean". After the study of this article, I believe you have a deeper understanding of how to use controller to transmit the formal value of boolean. 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report