Home Spring Spring3.2 Defining handler methods using @RequestMapping annotation
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Defining handler methods using @RequestMapping annotation

Advertisement
In this section, you will learn about defining new handler methods using @RequestMapping annotation.

Defining handler methods using @RequestMapping annotation

In this section, you will learn about defining new handler methods using @RequestMapping annotation.

Handler methods are used to handle the requests which is passed to these method through @RequestMapping annotation. Or in other words, we can say that "@RequestMapping annotation  is used to map the requested URL to the associated method to handle the request".

Handler methods have very flexible signatures. The arguments order can be interchanged frequently except BindingResult argument.

Supported argument types of handler methods

Given below the supported argument types of handler methods :

  • Request or response objects
    Request type can have ServletRequest or HttpServletRequest and response type can have ServletResponse or HttpServletResponse.

  • Session object of type HttpSession is supported.

  • org.springframework.web.context.request.WebRequest or org.springframework.web.context.request.NativeWebRequest

  • java.util.Locale

  • java.io.InputStream / java.io.Reader

  • java.io.OutputStream / java.io.Writer

  • java.security.Principal

  • For access to URI template variables, use of @PathVariable annotated parameters is legal.

  • For access to name-value pairs located in URI path segments, use of @MatrixVariable annotated parameters is legal

  • For Servlet request parameters, use of @RequestParam annotated parameters is legal

  • For accessing Servlet request HTTP headers, use of @RequestHeader annotated parameters is legal.

  • For accessing HTTP request body, use of @RequestBody annotated parameters is legal.

  • For "multipart/form-data",use of @RequestPart annotated parameters is legal.

  • You can use HttpEntity<?> parameters for accessing Servlet request HTTP headers and contents.

  • java.util.Map / org.springframework.ui.Model / org.springframework.ui.ModelMap

  • In case of a redirect, org.springframework.web.servlet.mvc.support.RedirectAttributes is used to specify the exact set of attributes.

  • org.springframework.validation.Errors / org.springframework.validation.BindingResult validation results

  • org.springframework.web.bind.support.SessionStatus status handle

  • org.springframework.web.util.UriComponentsBuilder for building a URL

  • To bind request parameters to bean properties, Command or form objects can be used.

Illegal sequence of BindingResult and @ModelAttribute

Given below sequence is invalid :

@RequestMapping(method = RequestMethod.POST)
public String submitProcessing(@ModelAttribute("employee") Employee employee,
Model model, BindingResult result) { 
............
}

The ordering of BindingResult and @ModelAttribute must be as follows :

@RequestMapping(method = RequestMethod.POST)
public String submitProcessing(@ModelAttribute("employee") Employee employee,
BindingResult result, Model model) {
.........
}

Handler method 's supported return type

  • ModelAndView object

  •  Model object

  • Map object

  • View object

  • String value

  • void if the method handles the response itself

  • To provide access to the Servlet response HTTP headers and contents, HttpEntity<?> or ResponseEntity<?> object can be used.

  • If the application wants to produce the return value asynchronously, a Callable<?> can be returned.

  • When the application wants to produce the return value from a thread of its own choice, DeferredResult<?> can be returned.



Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 9, 2013

Related Tutorials

Ask Questions?    Discuss: Defining handler methods using @RequestMapping annotation  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com