Sunday, August 9, 2015

Spring MVC - Refreshing basics

Spring MVC is a way provided by Spring to handle and operate on web requests. 

The following steps are performed for any web requests. 
  • The request is sent to the Dispatcher servlet
  • The Dispatcher servlet passes the request to the handler mapping component. 
  • The handler mapping component returns the respective controller. 
  • The Controller receives the request along with the request attributes, performs the business logic and returns back the Model and view. 
  • The dispatcher servlet resolves the view name using the View resolver component.
  • The dispatcher servlet now knows which view to forward to, hence returns the respective view to the page. 

What should you do to get started using Spring mvc
  • Include required maven artifacts such as spring-webmvc and spring-web in your pom.xml
  • Add the component scanning class, just to tell container which classes to look out for. 
  • @EnableWebMvc to enable spring mvc in your application configuration file. 
  • Create Spring controllers: 
    • Tell the class that this is a controller class using @Controller
    • Define @RequestMapping annotation at the class level 
    • Define @RequestMapping annotation at the method level 
  • Create JSP views and provide view resolver mapping. 
  • Provide implementation in the controller method by accepting the input parameters, processing it and returning response. 

Signature of the controller method: 

The signature of the method of the controller is open ended, hence it can accept a variety of input parameters. Following are the possible parameters. 
  • The servlet request or servlet response
  • Request parameters annotated by @RequestParam annotation
  • Model attributes annotated by @ModelAttribute annotation
  • Cookie value in the incoming request using @CookieValue annotation
  • Map or model map for the user to had his own attributes. 
  • Errors or BindingResult for the user to add errors / validation result. 
and more options such as SessionStatus. 

@RequestMapping options: 
@RequestMapping("/member/add”)
@RequestMapping(value={"/member/remove","/member/delete"}, method=RequestMethod.GET)
@RequestMapping(value= "processUser", method =  RequestMethod.POST)

Note
  • You can use handler interceptor to provide implementation for pre / post processing of web requests. 
  • You can use resource bundle & message properties to implement internationalization. 
  • You can handle all the exceptions in one place using HandlerExceptionResolvers. 
  • You can publish data as rest services using @Requestmapping and @PathVariable. The return type can be XML or JSON depending upon the configuration 
  • You can produce JSON responses using Jackson mapping and @ResponseBody annotation. 
  • You can also access REST services using RESTTemplate class, there are multiple methods available to perform REST operation such as getForObject, put, delete, postForEntity etc., 
  • You can publish RSS feds using jdom. 


No comments:

Post a Comment