Displaying and processing a form

To display a form and retrieve the data the user entered when it's submitted, use a first controller method to display the form. Use a second controller method to process the form data when the form is submitted.

How to do it…

Here are the steps to display and process a form:

  1. Create a controller method to display the form:
    @RequestMapping("/addUser")
    public String addUser() {
      return "addUser";
    }
  2. Create a JSP with an HTML form:
    <form method="POST">
      <input type="text" name="firstName" />
      <input type="submit" />
    </form>
  3. Create another controller method to process the form when it's submitted:
    @RequestMapping(value="/addUser", method=RequestMethod.POST) public String addUserSubmit(HttpServletRequest request) { String firstName ...

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.