- value - identifying the path mapping URL such as: /events/search.
- method - The HTTP request method to map to: GET, POST, PUT, DELETE etc.
- params - The request parameters, for example 'day=Tuesday' from a URL such as x/y/z?day=Tuesday.
- headers - Details of the request header, for example 'content-type=text/*'.
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String getCreateForm(Model model) {
Handler method signatures are particularly flexible and take a large number of different parameters in any order. The only fixed rule is that a validation result object such as Errors or BindingResults must follow the the command object to which it refers:
@RequestMapping(value = "/search", method = RequestMethod.POST)
public String doSearch(SearchCriteria criteria, BindingResult bindingResult, Model model) {
A full list of parameters and possible return types is available in the Spring 3 Javadocs for @RequestMethod. From this documentation, you will see that you do have access to the usual servlet classes such as HttpServletRequest, but wisely, the on-line examples steer you away from using these kinds of objects in favour of simple Model based objects:
@RequestMapping(method = RequestMethod.GET, params="day")
public String getAppointmentsForDay(@RequestParam @DateTimeFormat(iso=ISO.DATE) LocalDate day, Model model) {
I guess that as a rule, you should prefer using Models, command objects, @PathVariable and @RequestParam {value} to using Servlet HttpXXXXX classes.
Although not stated in the documentation, it seems to be convention that the command object bean is an input parameter, taking input from the previous POST or GET and that any Model parameter is an output parameter, which means that the usual basic method scenario should be something like:
- Read values from command object.
- Use these methods to do something (like access a database).
- Get your results and stuff them into the Model with an appropriate key if necessary.
- Return the name of your next View.
- Use the data from the model in your JSP.
Links Example MVC Code
http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/
http://blog.springsource.com/2010/07/22/spring-mvc-3-showcase/
http://blog.springsource.com/2009/12/21/mvc-simplifications-in-spring-3-0/
No comments:
Post a comment