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

What are the ways in which springMVC can obtain request parameters

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the ways in which springMVC obtains request parameters". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "what are the ways in which springMVC obtains request parameters" together!

preliminary understanding

Values for request parameters can be given directly in the request address

It can also be submitted as a form, for example:

User Name: Password: Hobbies:C++ Java C submitted

The form looks like this:

servletAPI

HttpServletRequest is what we learned in javaweb, it can also be used in our springMVC framework, HttpServletRequest as a parameter of the controller method, in this case HttpServletRequest type parameters represent the object encapsulating the request message of the current request

The specific codes are as follows:

@RequestMapping("/one") public String testParam(HttpServletRequest request) { String username = request.getParameter("username"); String password = request.getParameter("password"); String[] hobbies = request.getParameterValues("hobby"); return "success"; }

This method is called when we submit the form after we have set the correct request address in the form, and then get the corresponding parameter value according to the name attribute value of the form. As you can see, getParameter is to get the value of a single attribute, and getParameterValues is to get the values of multiple attributes.

method parameters

Next, I introduce the second method.

In the parameter position of the controller method, set the parameter with the same name as the request parameter. When the browser sends a request and matches the request mapping, the request parameter will be assigned to the corresponding parameter in DispatcherServlet.

The specific codes are as follows:

@RequestMapping("/two") public String testTwo(String username, String password, String hobby) { System.out.println("username:" + username + ",password:" + password + ",hobby:" + hobby); return "success"; }

Of course, it could also be written like this:

@RequestMapping("/two") public String testTwo(String username, String password, String[] hobby) { System.out.println("username:" + username + ",password:" + password + ",hobby:" + hobby); return "success"; }

Either way, the first group converts the values of multiple parameters into an array; the second group puts the parameter values into an array and needs to iterate to get the result itself.

@RequestParam

Recommended to use this method, very easy to use.

@RequestParam is a mapping relationship between request parameters and parameters of controller methods

@RequestMapping("/three") public String testThree(@RequestParam(value = "username", required = false, defaultValue = "hello") String username, @RequestParam(value = "password") String password, @RequestParam(value = "hobby") String hobby) { System.out.println("username:" + username + ",password:" + password + ",hobby:" + hobby); return "success"; }

The @RequestParam annotation has three attributes:

value: Specifies the parameter name of the request parameter assigned to the parameter

required: Sets whether this request parameter must be transmitted, default is true

If set to true, the current request must transmit the request parameter specified by value. If the request parameter is not transmitted and the defaultValue attribute is not set, the page will report Error 400: Required.

String parameter 'xxx' is not present; if set to false, the current request does not have to transmit the request parameter specified by value; if not transmitted, the value of the parameter identified by the annotation is null

defaultValue: Regardless of whether the required attribute value is true or false, when the request parameter specified by value is not transmitted or the value transmitted is "", the default value is used to assign a value to the parameter.

The results are correct:

entity class

Finally, you can set a parameter of entity type in the parameter position of the controller method. In this case, if the parameter name of the request parameter transmitted by the browser matches the attribute name in the entity class, the request parameter will be assigned to this attribute.

Here is the code:

public class User { private String username; private String password; private String[] hobby;}

The getters, setters and toString of this user entity class are not listed

@RequestMapping("/four") public String testFour(User user) { System.out.println(user); return "success"; }

Test it:

After submission, the results of the run are also completely correct:

Thank you for reading, the above is the "springMVC to obtain the request parameters of the way what" content, after learning this article, I believe we have a deeper understanding of the springMVC to obtain the request parameters of the way this problem, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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