Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 controller method and get the submitted value for name directly from it:

@RequestMapping("processForm")
public void processForm(HttpServletRequest request) {
  String name = request.getParameter("name");

See also

Refer to the Saving form values into an...