Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to get request parameters in Spring3 MVC

Shulou Source: shulou.com Published: 2022-06-02 06:06:11 09月25日 Update

This article is about how to get the request parameters in Spring3 MVC. The editor thinks it is very practical, so I hope you can get something after reading this article. Let's take a look at it with the editor.

Get the parameters in the path through @ PathVariabl

@ RequestMapping (value= "user/ {id} / {name}", method=RequestMethod.GET) public String printMessage1 (@ PathVariable String id,@PathVariable String name, ModelMap model) {System.out.println (id); System.out.println (name); model.addAttribute ("message", "111111"); return "users";}

For example, when accessing the user/123/lei path, execute the above method, where the parameter id=123,name=lei

@ ModelAttribute gets the FORM form data of the POST request

Java Pojo is as follows

Public class Pojo {private String a; private int b;}

Java Controller is as follows

@ RequestMapping (method = RequestMethod.POST) public String processSubmit (@ ModelAttribute ("pojo") Pojo pojo) {return "helloWorld";}

@ RequestBody gets the FORM form data of the POST request

@ RequestBody receives a string of Json objects, not a Json object. However, in ajax requests, Json objects are often passed, and later found that the object can be turned into a string by JSON.stringify (data). At the same time, specify dataType: "json", contentType: "application/json" when making an ajax request so that you can easily pass an object or List to the Java side, and use @ RequestBody to bind the object or List.

Js code

$(document) .ready (function () {var saveDataAry= []; var data1= {"userName": "test", "address": "gz"}; var data2= {"userName": "ququ", "address": "gr"}; saveDataAry.push (data1); saveDataAry.push (data2) $.ajax ({type: "POST", url: "user/saveUser", dataType: "json", contentType: "application/json", data:JSON.stringify (saveData), success:function (data) {

}))

Java code

@ RequestMapping (value = "saveUser", method = {RequestMethod.POST}}) @ ResponseBody public void saveUser (@ RequestBody List users) {userService.batchSave (users);}

The difference between @ ModelAttribute and @ RequestBody annotations is that @ ModelAttribute annotations can get the return value directly at the front end.

@ Controllerpublic class Hello2ModelController extends BaseController {

@ RequestMapping (value = "/ helloWorld2") public String helloWorld (@ ModelAttribute ("myUser") User user) {user.setName ("Lao Wang"); return "helloWorld";}}

Key in model is myUser, and the foreground can obtain the corresponding user attributes directly through ${myUser.xx}.

Get it directly with HttpServletRequest

@ RequestMapping (method = RequestMethod.GET) public String get (HttpServletRequest request, HttpServletResponse response) {System.out.println (request.getParameter ("a")); return "helloWorld";}

Bind request parameters with the annotation @ RequestParam

An exception occurs when the request parameter a does not exist, which can be solved by setting the property required=false

For example: @ RequestParam (value= "a", required=false)

Controller is as follows

@ RequestMapping (value = "/ requestParam", method = RequestMethod.GET) public String setupForm (@ RequestParam ("a") String a, ModelMap model) {System.out.println (a); return "helloWorld";} above is how to obtain request parameters in Spring3 MVC. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Parameters objects annotations code characters strings attributes data more knowledge articles forms paths different practical foreground front end variables can be passed at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Shulou Tech Info OPPO Reno Microsoft