4

Spring documentation says:

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

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

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

Is this a bug in the documentation? Can someone please cite me some documentation that describes exactly what @RequestParam is used for, what it cannot be used for, and how it gets populated?

Can I use @RequestParam for POST methods to get the form values? If I can't use @RequestParam, what else can I use? I'm trying to avoid calling request.getParameter("key").

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
supertonsky
  • 2,563
  • 6
  • 38
  • 68
  • Related question: [java - Spring MVC: please explain difference between `@RequestParam` and `@ModelAttribute` - Stack Overflow](https://stackoverflow.com/questions/29370581/spring-mvc-please-explain-difference-between-requestparam-and-modelattribute) – Jason Law Apr 01 '20 at 07:19

3 Answers3

6

It works with posts too. Can you post your method body and you html?

Arash
  • 11,697
  • 14
  • 54
  • 81
  • How can I explain this stupidity, we were using custom tags actually. I thought I was using the "name" attribute. Yes it works. -_- – supertonsky Dec 28 '11 at 06:46
0

Yes it works perfectly with post method too. you can mention the method attribute of @RequestParam as RequestMethod=POST. Here is the code snippet

@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 
)
nempoBu4
  • 6,521
  • 8
  • 35
  • 40
Amit
  • 1
0

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

Example: - controller that calls JSP-page, it should add objects to a 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 that:

Track Title *:

  • Controller that processes form submission;

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

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

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

user07
  • 319
  • 2
  • 8