What is @RequestParam and how is it populated?

Spring documentation says:

Use the @RequestParam annotation to bind request parameters to a method in your controller.

Source: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

AFAIK, query parameters are variables that are retrieved from query strings if the query method is GET. They are also variables derived from form values ​​when the request method is POST. I checked this using a simple JSP that displays request parameters using the request.getParameter ("key") method.

But it seems to me that @RequestParam only works with GET method requests. It can receive values ​​only from query strings.

Is this a mistake in the documentation? Can someone please give me some documentation that describes what @RequestParam is used for, why it cannot be used, and how it is populated?

Is it possible to use @RequestParam methods for POST to get form values? If I cannot use @RequestParam, what else can I use? I am trying to avoid calling request.getParameter ("key").

+4
source share
3 answers

It also works with messaging. Can you post your method body and you are html?

+6
source

Yes, it works great with the mail method. you can specify the attribute of the @RequestParam method as RequestMethod=POST . Here is a snippet of code

 @RequestMapping(value="/register",method = RequestMethod.POST) public void doRegister ( @RequestParam("fname") String firstName, @RequestParam("lname")String lastName, @RequestParam("email")String email, @RequestParam("password")String password ) 
0
source

Instead of @RequestParam , which is associated with a single form value, you can use the @ModelAttribute annotation and bind to the entire object. But it should be used in conjunction with form or bind Spring JSTL.

Example: - the controller that invokes the JSP page must add objects to the model:

 @RequestMapping(value="/uploadForm", method=RequestMethod.GET) 

public String showUploadForm (model model) {

 Artist artist = new Artist(); Track track = new Track(); model.addAttribute("artist", artist); model.addAttribute("track", track); return "uploadForm"; 

}

  • JSP might look something like this:

Name of the track *:

  • The controller processing the form submission;

    @RequestMapping (value = "/ uploadToServer", method = RequestMethod.POST)

    public String uploadToServer (@ModelAttribute ("artist") Artist, @ModelAttribute (track) Track track) {....}

Here I found a good explanation for using @ModelAttribute annotation - krams915.blogspot.ca

0
source

Source: https://habr.com/ru/post/1388240/


All Articles