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 introduces "what are the ways of transmitting and receiving parameters in Spring MVC". In the daily operation, I believe that many people have doubts about the way of transmitting and receiving parameters in Spring MVC. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what are the ways of transmitting and receiving parameters in Spring MVC?" Next, please follow the editor to study!
1. Receive the request parameter @ RequestMapping ("/ login") public String login (User user, Model model) {if ("admin" .equals (user.getName ()) & & "123456" .equals (user.getPwd () {model.addAttribute ("message", "login successful") through the entity Bean; return "main" / / Log in successfully, jump to the home page} else {model.addAttribute ("message", "wrong user name or password"); return "login";}} II. Receive request parameters through the parameters of the processing method
To receive the request parameters through the parameters of the processing method is to write the form parameters directly to the parameters of the corresponding method of the controller class, that is, the parameter name is exactly the same as the request parameter name. Receive parameter mode applies to GET and POST submission requests
@ RequestMapping ("/ login") public String login (String name, String pwd, Model model) {if ("admin" .equals (user.getName ()) & & "123456" .equals (user.getPwd () {model.addAttribute ("message", "login successful"); return "main" / / Log in successfully, jump to home page} else {model.addAttribute ("message", "wrong username or password"); return "login";}} III. Receive request parameter @ RequestMapping ("/ login") public String login (HttpServletRequest request, Model model) {String name = request.getParameter ("name"); String pwd = request.getParameter ("pwd") via HttpServletRequest. If ("admin" .equals (name) & & "123456" .equals (pwd)) {model.addAttribute ("message", "login successful"); return "main"; / / login successful, jump to main.jsp} else {model.addAttribute ("message", "wrong username or password"); return "login" Receive the request parameters in URL through @ PathVariable
Get the parameters in URL through @ PathVariable. The sample code is as follows.
@ RequestMapping ("/ login") public String login (@ RequestParam String name, @ RequestParam String pwd, Model model) {if ("admin" .equals (name) & & "123456" .equals (pwd)) {model.addAttribute ("message", "login successful"); return "main" / / Log in successfully, jump to the home page} else {model.addAttribute ("message", "wrong user name or password"); return "login";}} 5. Receive request parameters through @ RequestParam
Use the @ RequestParam annotation at the method input to specify its corresponding request parameters. @ RequestParam has the following three parameters:
Value: parameter name
Required: whether it is required. Default is true, which means that the corresponding parameter name must be included in the request. If it does not exist, an exception will be thrown.
DefaultValue: parameter default
@ RequestMapping ("/ login") public String login (@ RequestParam String name, @ RequestParam String pwd, Model model) {if ("admin" .equals (name) & & "123456" .equals (pwd)) {model.addAttribute ("message", "login successful"); return "main" / / Log in successfully, jump to the home page} else {model.addAttribute ("message", "wrong user name or password"); return "login";}} VI. Receive request parameters via @ ModelAttribute
The @ ModelAttribute annotation is used to encapsulate multiple request parameters into a single entity object to simplify the data binding process and to automatically expose it as model data for use in the view page representation. Receive request parameters through entity bean simply encapsulates multiple request parameters into an entity object without exposing them as model data
@ RequestMapping ("/ login") public String login (@ ModelAttribute ("user") User user, Model model) {if ("admin" .equals (name) & & "123456" .equals (pwd)) {model.addAttribute ("message", "login successful"); return "main" / / Log in successfully, jump to the home page} else {model.addAttribute ("message", "wrong user name or password"); return "login";}} at this point, the study on "what are the ways for Spring MVC to transmit and receive parameters" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.