Spring MVC : Controllers
What are Controllers?
- Controllers are usually JavaBeans which interprets request. After DispactherServlet receives a request and resolves locale, resource etc it delegates the request to controllers.
- Controller processes user request and populates model object with result and then represented the same to the user by the view.
- In some cases it invoke business logic or webservice.
- Any class can be defined as controller by using annotation @Controller along with @RequestMapping. No need to extend specific base classes or implement specific interfaces.
- @Controller annotation acts as stereotype. This annotation tells the dispatcher servlet to scan the class for mapped method. (A method with @RequestMapping annotation.)
One of the important annotation which is required in Controller is @RequestMapping.
This annotation is for mapping web requests onto specific handler classes and/or handler methods. This annotation tell the container which method will handle the request.
Methods which are annotated with @RequetMapping are allowed to have very flexible signatures.They may have arguments of the following types.
|1||ServletRequest / HttpServletRequest OR
PortletRequest / ActionRequest / RenderRequest
|Request and/or response objects (Servlet API or Portlet API)|
|2||HttpSession or PortletSession||This argument makes sure the handler method will have corresponding session.|
|3||WebRequest or NativeWebRequest||Allows for generic request parameter access as well as request/session attribute access.|
|4||LocaleResolver||Locale for the current request locale|
|5||InputStream / Reader||To access request ‘s content|
|6||OutputStream / Writer||For generating response content.|
|7||HttpEntity<?> (Servlet-only)||For access to the Servlet request HTTP headers and contents.|
|8||Map / Model / ModelMap||For populating the implicit model for the view.|
|9||RedirectAttributes (Servlet-only, @MVC 3.1-only)||To specify the exact set of attributes to use in case of a redirect
and also to add flash attributes
|10||Command/form objects||To bind parameters|
|11||Errors / BindingResult||Validation results for a preceding command/form object|
|12||SessionStatus||Status handle for marking form processing as complete|
|13||UriComponentsBuilder (Servlet-only, @MVC 3.1-only)||for preparing a URL relative to the current request’s host, port, scheme, context path, and the literal part of the servlet mapping.|
|14||@PathVariable||For access of URI template values.|
|15||@RequestParam||To bind request parameters to a method parameter in your
|16||@RequestHeader||For access to specific Servlet/Portlet request HTTP headers.|
|17||@RequestBody (Servlet-only)||For access to the Servlet request HTTP contents.|
|18||@RequestPart (Servlet-only, @MVC 3.1-only)||For access to the content of a part of “multipart/form-data”
|19||@MatrixVariable||For access to name-value pairs located in URI path segments|
The following return types are supported for handler
|1||ModelAndView Object||Implicit model and view gets returned.|
|2||Model Object||View is resolved by RequestToViewNameTranslator and model is implicit.|
|3||Map Object||Map object give the model and view is resolved by RequestToViewNameTranslator.|
|4||View Object||View object is returned and model is implicit.|
|5||String value||String value is interpreted as logical view and model is implicit.|
|6||@ResponseBody Annotated (Servlet -only)||In this case the method will write response to HTTP body|
|7||HttpEntity OR ResponseEntity||This provides access to HTTP header and response.|
|8||void||Response is handled by method or View is resolved by RequestToViewNameTranslator|
|9||Callable||Used by Spring MVC to obtain the return value asynchronously in a separate thread transparently managed by Spring MVC on behalf of the application.|
|10||Any object||This object is considered as a single model attribute.|
Example for each of return type and method signature will be discussed in separate post.