We call Model-2 architecture as MVC (Model View Controller) architecture but Model-1 has Model-1. There are three main components exists in Model 2 architecture: the model, the view, and the controller. Initially the term Model2 is used in the JavaServer Pages Specification version 0.92.
The Model layer is the most important part of the MVC paradigm.The Model is implemented by the Action component in Struts2. Model is used to implement all the application data and business logic of web based application or persistence logic. It represents the information of the application. Anything that an enterprise web application persists becomes a part of Model layer. It is the work of Java developer to develop a model. There are different technologies available in Java to create a model.
1. Java Bean
2. Enterprise Java bean
4. Web services
In Struts 2, the model is implemented by the Action component.
It is used to implement presentation logic of a web based application. The View layer queries the Model layer for its content and renders it. View is the work of web designer to create a view component, like Button, Textfield etc. The view is independent and it remains same even if the Application Logic or Business Logic undergoes change. There are many technologies available to implement presentation logic. A View in Struts 2 can be:
4. Swing and many more..
It is used to implement controlling logic or Integration Logic of a web based application. It defines the control flow and execution flow of a web application. Servlet is the only technology to implement controller in a web application. In Struts 2, the role of the controller is played by the StrutsPrepareAndExecuteDisptacher. Filter Dispatcher examines incoming request to determine the Action that will handle the request.
Integration logic is the central logic of the web application which takes the request from client, passes then to model layer resources, gather the results from model layer resources and passes the results to view layer resources. This logic controls and monitors every activity in a web application execution. All interaction between the View layer and the Model layer is managed by the Controller.
In Model View Architecture we integrate all the 4 logic
1. Presentation Logic (View)
2. Business Logic
3. Application Logic and Data Logic (Model)
4. Controlling Logic (Controller)
In a single JSP or SERVLET any change in one logic can affect the other logic.
In MVC Architecture, a servlet acts as a controller and it performs the common operations for all requests like input verification, session management, security etc. We call the servlet as a controller because it knows the entire flow of the web application.
The Controller finds appropriate business logic component for the given request and it will set the data to that component we call that business logic component as a model. By depending on the logic name/Hint returned by the model, the controller selects an appropriate view component this view component get response data from the model and takes this and point to the client browser.
According to MVC Architecture, servlet is a controller, Model is a Bean and JSP is a view.
Advantages of MVC Architecture:
1. Complexity is reduced, because each component has specific logic.
2. We can modify one component, by without affecting the other component.
Rules to be follow while constructing MVC Application:
1. In a web application, there can be any number of model components and any number of view components but there must be a single controller (servlet). This type of controller we call as front Controller.
2. We cannot do the direct linking between two pages, if we directly link two pages we are violating MVC principal because according to MVC the control flow of an application should be maintained controller only.
3. According to MVC architecture model and view component should not talk each other directly. The communication from view to Model and model to view should be done through the intermediator as controller.
4. In MVC, each component should contain its specific logic, we can't mix-up some other logic into that component.
Importance of MVC Application:
1. While developing web application is Real-time, if one developer wants to understand the flow created by another developer then the entire flow of the application should be maintained at a single place.
2. If an application, follows an MVC principal then it is very simple to increase an application or even decrease an application when it is needed or required.
3. The application implemented using MVC can support different types of client at runtime.
4. While using MVC, it is recommended that use servlet as a controller bean as model and JSP is as view.