views:

28

answers:

1

So when you go to /appointments the get() action is called, so then would the view be get.jsp (assuming you are using .jsp, and assuming you are mapping action names to views)?

And what about the getnewform? It seems to be returning an object? Is that basically passed into the view?

@Controller @RequestMapping("/appointments") public class AppointmentsController {

private final AppointmentBook appointmentBook;

@Autowired
public AppointmentsController(AppointmentBook appointmentBook) {
    this.appointmentBook = appointmentBook;
}

@RequestMapping(method = RequestMethod.GET)
public Map<String, Appointment> get() {
    return appointmentBook.getAppointmentsForToday();
}

@RequestMapping(value="/{day}", method = RequestMethod.GET)
public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
    return appointmentBook.getAppointmentsForDay(day);
}

@RequestMapping(value="/new", method = RequestMethod.GET)
public AppointmentForm getNewForm() {
    return new AppointmentForm();
}

@RequestMapping(method = RequestMethod.POST)
public String add(@Valid AppointmentForm appointment, BindingResult result) {
    if (result.hasErrors()) {
        return "appointments/new";
    }
    appointmentBook.addAppointment(appointment);
    return "redirect:/appointments";
} } 

In the example, the @RequestMapping is used in a number of places. The first usage is on the type (class) level, which indicates that all handling methods on this controller are relative to the /appointments path. The get() method has a further @RequestMapping refinement: it only accepts GET requests, meaning that an HTTP GET for /appointments invokes this method. The post() has a similar refinement, and the getNewForm() combines the definition of HTTP method and path into one, so that GET requests for appointments/new are handled by that method.

+2  A: 

@RequestMapping-annotated methods can return a wide variety of objects, including a View, a Model, a Map, a String, and so on. These return values are interpreted by ServletHandlerMethodInvoker.getModelAndView(), which constructs a ModelAndView objects based on that return value.

In cases where the return value does not specify a view name (in your example, every method other than add() returns no view name), then Spring will attempt to automatically resolve the view name. By default, this is done by DefaultRequestToViewNameTranslator, which uses the information about the request to choose the view name. The examples in the javadoc are:

  • http://localhost:8080/gamecast/display.html -> display
  • http://localhost:8080/gamecast/displayShoppingCart.html -> displayShoppingCart
  • http://localhost:8080/gamecast/admin/index.html -> admin/index

Note that the selected view name is not based upon the information in the @RequestMapping methods, but on the properties of the request itself.

skaffman
so the call to new AppointmentForm() will resolve to a view, and pass the AppointmentForm as an attribute/model to the view?
Blankman
@Blankman: Correct
skaffman