-1

In the following code,

snapshot of @RequestMapping method

`// Get addLocation.jsp.
@RequestMapping(value="/add-location", method=RequestMethod.GET)
public ModelAndView addLocationJSP() {
    System.out.println("Location: LocationController.addLocationJSP()");
    return new ModelAndView("addLocation", "location", new Location());
}

// Submit addLocation.jsp form.
@RequestMapping(value="/submit-location", method=RequestMethod.POST)
public ModelAndView submitLocation(@ModelAttribute("location") Location location) {
    System.out.println("Location: LocationController.submitLocation()");
    locationService.saveLocation(location);
    return new ModelAndView("confirmSubmit");
}`

what, exactly, do value="", @ModelAttribute(), and return new ModelAndView() do?

How is the role of value="add-location" different from the first argument of @ModelAndView("addLocation")?

Rachel
  • 3
  • 1

1 Answers1

0
  • @RequestMapping annotation is used to map a URI to a method in your controller. The parameters you need for this tag are "value", which represents the URI you want to map with the method, and method, which represents the HTTP type of the request. When you want to map a GET request you can omit the parameter, because its the default method (so, in your first method you should write: @RequestMapping("/add-location") ). When the user navigating your web app comes to the /add-location URI, then the addLocationJSP method will be executed.
  • @ModelAttribute annotation is the one that binds a method parameter or method return value to a named model attribute, exposed to a web view. In other words, you should have a JSP page containing a form, with a POST method, used for populating a Location object. Thanks to ModelAttribute you can bind the object between view and controller, setting in the Location object the values collected by the inputs of the form.
  • In @ModelAndView you need the name of the view you want to display after the method is executed; instead, in the value parameter, you need the URI you want to intercept.

I think you need to study some basic example (you can find many other tutorials, I linked just one of man most)

andy
  • 269
  • 1
  • 12
  • 22