The Spring Web model-view-controller (MVC) framework is designed around a
DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view
resolution, locale and theme resolution as well as support for uploading files.
DispatcherServlet is an expression of the “Front Controller” design pattern. Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory.
All default mappings of DispatcherServlet are mentioned in DispatcherServlet.properties.
org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager
MVC controllers uses annotations such as @RequestMapping, @RequestParam, @ModelAttribute
DispatcherServlet invokes handlers (controllers) using handler mapping.
@RequestMapping can be on class level or method level. It can map url as absolute value or parameterized value or RegEx value. e.g /home, /id/{val1}, /id/[0-9]*
@PathVariable annotated parameters for access to URI template variables. They will be merged in the model before rendering so no need to add path variables in model.
• @RequestParam annotated parameters for access to specific Servlet request parameters. Parameter
values are converted to the declared method argument type. Spring can auto populate bean based on request param or uri varaibales.
e.g. /path?page=foo&
@ModelAttribute can be added on method as well as method parameter. @ModelAttribute without any parameter will invoke each time for any request to that controller. It will be useful for populating drop down boxes or fetching static data like country list, currency list etc.
@ModelAttribute methods in a controller are invoked before @RequestMappingmethods, within the same controller.@SessionAttribute The type-level
@SessionAttributes annotation declares session attributes used by a specific handler. This will typically list the names of model attributes or types of model attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans between subsequent requests.@ResponseBody for Ajax requests add output into response using this annotation.
HttpEntity or ResponseEntity gives access to servlet response Http headers & contents.
If JSP fields are bound by spring tags, then on submission fields will be populated into bean by spring.
HandlerInterceptors & HandlerAdapters can be used to preHandle(), postHandle(), afterCompletion() of HandlerMapping.
Chaining View Resolver :- spring supports multiple view resolver to override specific
views in certain circumstances.
RedirectView :- The RedirectView issues an HttpServletResponse.sendRedirect() call that returns to the client browser as an HTTP redirect. By default all model attributes are considered to be exposed as URI template variables in the redirect URL. Same functionality of RedirectView is achieved by adding prefix "redirect:" in controller.
e.g. return "redirect:/redirect/{account}";
"forward: prefix" can be used for forwarding request.
FlashMap attributes are used for mainly redirect url scenario.
Table 16.1. Special bean types in the WebApplicationContext that DispatcherServlet uses
| Bean type | Explanation |
|---|---|
| HandlerMapping | Maps incoming requests to handlers and a list of pre- and post-processors (handler interceptors) based on some criteria the details of which vary by HandlerMapping implementation. The most popular implementation supports annotated controllers but other implementations exists as well. |
| HandlerAdapter | Helps the DispatcherServlet to invoke a handler mapped to a request regardless of the handler is actually invoked. For example, invoking an annotated controller requires resolving various annotations. Thus the main purpose of a HandlerAdapter is to shield theDispatcherServlet from such details. |
| HandlerExceptionResolver | Maps exceptions to views also allowing for more complex exception handling code. |
| ViewResolver | Resolves logical String-based view names to actual View types. |
| LocaleResolver &LocaleContextResolver | Resolves the locale a client is using and possibly their time zone, in order to be able to offer internationalized views |
| ThemeResolver | Resolves themes your web application can use, for example, to offer personalized layouts |
| MultipartResolver | Parses multi-part requests for example to support processing file uploads from HTML forms. |
| FlashMapManager | Stores and retrieves the "input" and the "output" FlashMap that can be used to pass attributes from one request to another, usually across a redirect. |
Handler Mappings :- In previous versions of Spring, users were required to define one or more
HandlerMapping beans in the web application context to map incoming web requests to appropriate handlers. With the introduction of annotated controllers, you generally don’t need to do that because the RequestMappingHandlerMapping automatically looks for @RequestMapping annotations on all @Controller beans.| Annotation | Meaning |
+------------+-----------------------------------------------------+
| @Component | generic stereotype for any Spring-managed component |
| @Repository| stereotype for persistence layer |
| @Service | stereotype for service layer |
| @Controller| stereotype for presentation layer (spring-mvc) |
@Component and @Bean do two quite different things, and shouldn't be confused.
@Component (and @Service and @Repository) are used to auto-detect and auto-configure beans using classpath scanning. There's an implicit one-to-one mapping between the annotated class and the bean (i.e. one bean per class). Control of wiring is quite limited with this approach, since it's purely declarative.
@Bean is used to explicitly declare a single bean, rather than letting Spring do it automatically as above. It decouples the declaration of the bean from the class definition, and lets you create and configure beans exactly how you choose. It is a method level annotation. It is used mainly for manually creating beans. Mostly creating bean from third party jar classes.
No comments:
Post a Comment