Sunday, 6 September 2015


MVC Page Life Cycle
There are six broader events which occur in MVC application life cycle below diagrams summarize it. Any web application has two main execution steps first understanding the request and depending on the type of the request sending out appropriate response. MVC application life cycle is not different it has two main phases first creating the request object and second sending our response to the browser. Creating the request object: -The request object creation has four major steps. Below is the detail explanation of the same. Step 1 Fill route: - MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the first thing is to fill the route table with routes collection. This filling of route table happens in the global.asax file. Step 2 Fetch route: - Depending on the URL sent “UrlRoutingModule” searches the route table to create “RouteData” object which has the details of which controller and action to invoke. Step 3 Request context created: - The “RouteData” object is used to create the “RequestContext” object. Step 4 Controller instance created: - This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class. Creating Response object: - This phase has two steps executing the action and finally sending the response as a result to the view.
Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts: Model - The lowest level of the pattern which is responsible for maintaining data. View - This is responsible for displaying all or a portion of the data to the user. Controller - Software Code that controls the interactions between the Model and View. MVC is popular as it isolates the application logic from the user interface layer and supports separation of concerns. Here the Controller receives all requests for the application and then works with the Model to prepare any data needed by the View. The View then uses the data prepared by the Controller to generate a final presentable response. The MVC abstraction can be graphically represented as follows. The model The model is responsible for managing the data of the application. It responds to the request from the view and it also responds to instructions from the controller to update itself. The view A presentation of data in a particular format, triggered by a controller's decision to present the data. They are script based templating systems like JSP, ASP, PHP and very easy to integrate with AJAX technology. The controller The controller is responsible for responding to user input and perform interactions on the data model objects. The controller receives the input, it validates the input and then performs the business operation that modifies the state of the data model. Struts2 is a MVC based framework. In the coming chapters, let us see how we can use the MVC methodology within Struts2.