3

In a a POST request handling method (annotated with @RequestMapping) of a Spring MVC controller, you can access the form values POST-ed from the form in two ways (correct?).

Use @ModelAttribute

You can use a model object (command) class that bundles all the form values together and use an argument annotated @Valid @ModelAttributeto pass in the form values. This seems to be the common way of processing forms.

 @RequestMapping(value = { "thing/{thing}/part/" }, method = RequestMethod.POST)
 public String processForm(
    @PathVariable("thing") final String thing,
    @ModelAttribute(value = "command") @Valid final Command command,
    final BindingResult bindingResult) {
    if (!bindingResult.hasErrors()) {
       final String name = command.getName();
       final long time = command.getTime().getTime();

       // Process the form

       return "redirect:" + location;
    } else {
       return "form";
    }
 }

Use @RequestParam.

You can annotate individual method arguments with @RequestParam.

 @RequestMapping(value = { "thing/{thing}/part/" }, method = RequestMethod.POST)
 public String processForm(
       @PathVariable("thing") final String thing,
       @RequestParam(value = "name", required=true) final String name,
       @RequestParam(value = "time", required=true) final Date time,
      final HttpServletResponse response) {
    // Process the form

    return "redirect:" + location;
 }

Why Use @ModelAttribute

So why bother using @ModelAttribute, given that it has the inconvenience of having to create an auxiliary command class? What are the limitations of using a @RequestParam.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • Annotating an argument with `@ModelAttribute` has nothing to do with getting form values. – a better oliver Nov 20 '14 at 08:37
  • @zeroflagL Errr? It is useful for nothing else. – Raedwald Nov 20 '14 at 13:00
  • 1
    If it is used with a parameter then that object is added to the model and optionally taken from the model, hence the name `Model Attribute`. The object will be populated with the form values with or without `@ModelAttribute`. – a better oliver Nov 20 '14 at 13:33

1 Answers1

4

Actually, there are more like at least four ways (@RequestParam, @ModelAttribute, an un-annotated Pojo parameter, and straight from the Request object)

The main reason to use a single structured parameter is if you have structured data with several fields. It's more convenient than using several @RequestParam params, and you can validate all at the same time. With @ModelAttributes you can retrieve a single object from session, database, or flashAttributes easily.

You can use existing Entities or Pojos with @ModelAttribute, you don't need to create a custom Form Backing Object necessarily.

But ya, if you just have one or two params, then @RequestParam is fine.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • 2
    Actually there are unlimited ways to handle incoming data. You didn't mention `@RequestBody` e.g., and you can write your own argument resolvers. `@ModelAttribute` isn't for reading form values. – a better oliver Nov 20 '14 at 08:48