Getting a submitted form value using a controller method argument

In this recipe, you will learn how to get the submitted form data using controller method arguments. This is convenient for simple forms that are not related to a domain object.

How to do it…

Add an argument annotated with @RequestParam to the controller method:

@RequestMapping("processForm")
public void processForm(@RequestParam("name") String userName) {
...

How it works…

The userName argument is initialized by Spring with the submitted value of the name form field.

@RequestParam can also retrieve URL parameters, for example, http://localhost:8080/springwebapp/processForm?name=Merlin.

There's more…

It's also possible to add the standard HttpServletRequest object as an argument of the ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.