Pages

Sunday, July 24, 2016

Spring MVC



What is Spring MVC....??

                              The diagram below gives a general idea of what MVC is and its usage.



The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications. 



Model =>
represents the underlying, logical structure of data in a software
directly manages the data, logic and rules of the application.

View =>
represents the elements in the user interface
responsible for rendering the model data through the generated HTML

Controller =>
represents the classes connecting the model and the view
        communicate between classes in the model and view

The DispatcherServlet


In the Spring Web MVC framework we need an interface which handles the HTTP requests and make then reach the above mentioned 3 components. All the HTTP requests coming to the application first reach to the DispatcherServlet and then from there the requests are handled accordingly.

Its worth discussing the MVC Architecture functionality to get a general idea of how the HTTP requests are being served by Spring Web MVC.

The following shows the step-wise functionality referred with the below diagram


  • The DispatcherServlet first receives the request.
  • The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request.
  • The Controller process the request by calling the appropriate service methods and returns a ModeAndView object to the DispatcherServlet. The ModeAndView object contains the model data and the view name.
  • The DispatcherServlet sends the view name to a ViewResolver to find the actual View to invoke.
  • Now the DispatcherServlet will pass the model object to the View to render the result.
  • The View with the help of the model data will render the result back to the user.


ican.png

This DispatcherServlet must be mapped before it can work with the correct functionality.


  • You need to map which requests are handled by which DispatcherServlet


  • This mapping is done by using a URL mapping in web.xml config file

  • Following shows the mapping for the HelloWeb DispatcherServlet
helloweb.png


  • <servlet> tag => initializes the dispatcherservlet named
                                                           HelloWeb .


  • <servlet-mapping> tag =>   indicates what URLs will be handled  by the which dispatcherservlet




  • Then the framework will try to load the application

       context from a file named [servlet-name]-servlet.xml

In this case it is : HelloWeb-servlet.xml

  • Following shows the above configured HelloWeb-servlet.xml dispatcherservlet.
  • Spring Configuration is done there
servlet-xml.png


Now lets  go through a simple Model, View and Controller  in  Spring MVC .
  • A class with @Controller is termed as the controller



handlermethod.png


  • Model is the business logic is defined inside the service method of the controller
  • Model is created within the method (in the method argument in above example)
  • Different model attributes are set and accessed by the view to present the output
  • In above model attribute set is message. And is accessed in the view view.png

Tips for writing a MVC Controller



01) Annotating a  class with @Controller is the simplest way of creating a controller

02) It returns the view name

03)For a controller to handle one request @RequestMapping at the beginning of the class

anno1.png
























04) For a controller to handle multiple different request @RequestMapping at the beginning of each method in the class

















05)  @RequestMapping  annotation can be used to specify multiple different URLs  to be handled by a single method



                        @RequestMapping({"/hello""/hi""/greetings"})


06) Specify the HTTP Request method in  @RequestMapping  annotation. 













07) Retrieve request parameters as regular parameters of the handler method by using @RequestParam
 annotation. This decouples the controller from the HttpServletRequest interface











Spring binds the method parameters username and password to the HTTP request parameters with the same names. 
That means you can invoke a URL as follows (if request method is GET):
http://localhost:8080/spring/login?username=scott&password=tiger

Some especial case scenarios of the @RequestParam annotation is shown below











































08) The simplest is returning  view name  from the model as a String as below







09) If additional data is needed to be sent to the view, return a  ModelAndView  object. There are 3 basic ways in declaring a ModelAndView object.


  • First method is to create a ModelAndView  object in handler method. The object holds the view name in its initiation











  • The second method is to declare ModelAndView object as a parameter of the handler method.
          Here view name is set for the model and objects are added to the model












  • declare as a parameter of type Map in the handler method. This map is used to store 
           objects for the  model










10) In case you want to redirect the user to another URL if a condition is met, 
just append redirect:/ before the URL. 











11) @ModelAttribute  is used to bind form fields to the form backing objects. 
       BindResult interface is used for validating form fields.





No comments:

Post a Comment