vendredi 6 septembre 2013

Spring MVC

The objective of this paper is to present the operating principle of spring MVC and the basis of this framework.

Principle of operation
Spring MVC framwork which is used to implement applications in the MVC design pattern. So, like any other MVC framework, Spring MVC is based on the principle described in the diagram below:


mvc schema
Turning now to the details: below the kinematics of running an application by Spring MVC.

(spring mvc)
The steps are:
1. The DispatcherServlet receives a request whose URI pattern is '/ mypage.html'.

2. The DispatcherServlet consults his Handler Mapping (Example: BeanNameUrlHandlerMapping) for the controller whose bean name is '/ mypage.html'.

3. The DispatcherServlet dispatches the request to the controller identified (Example: MyController)

4. The controller returns to DispatcherServlet a ModelAndView object as a parameter type having at least the logical view name to return.

5. The DispatcherServlet consults its View Resolver for him to find the view that the logical name 'myPage'. Here type is InternalResourceViewResolver View Resolver chooses.

6. The DispatcherServlet forwarde the request to the associated view. Here page / WEB-INF/jsp / myPage.jsp
(spring mvc)
NOTE: For fans of struts, the servlet DispatcherServlet Spring is the equivalent of Struts ActionServlet controller.
(spring mvc)
DispatcherServlet

This is the entry point of the application, the DispatcherServlet makes the first mapping of the application and distributes requests to the same servlet.
(spring mvc)
Example: Assuming we have this configuration in web.xml:
web
 With this configuration, all urls starting with our application myApplication be "dispatched" to this servlet.
(spring mvc)
Controller

The controller (the C in MVC) intercepts the request and return the appropriate view.
(spring mvc)
Spring MVC offers several controllers: AbstractController, AbstractController, AbstractCommandController, AbstractFormController, SimpleFormController, AbstractWizardFormController ...
(spring mvc)
Interface 'Controller' defines the basic behavior of a controller: intercept the request and return a 'ModelVue' that represents the model and the view.
(spring mvc)
MultiActionController: a controller whose methods will be launched on the basis of input URLs.
(spring mvc)
Handler Mappings

To map HTTP requests with the corresponding controller.
(spring mvc)
View Resolvers

Determines the name of the view.

Exmple: Returning to our example below describing the kinematics of the management applications from Spring MVC to determine the name of the jsp page that corresponds to the url / myPage we must add in the configuration file below:
servletxml1.jpg
 The handler of our control must return an object of ModelAndView type it is declared as follows:
(spring mvc)
return new ModelAndView ("myPage");
(spring mvc)
From the view name returned myPage eg ViewResolver find the corresponding jsp page that is in the "/ WEB-INF/jsp / folder" with the suffix ". Jsp"
(spring mvc)
  Conclusion

In this article we have seen the operating principle of spring MVC as well as the main components. In the next article we will implement our first application with spring MVC.

0 commentaires:

Enregistrer un commentaire