Struts Tutorial «Prev  Next»


Model View Controller used for Struts

MVC The MVC (Model-View-Controller) architecture the client request is first intercepted by a servlet referred as controller servlet. this servlet handles the initial processing of the request and determines which JSP page to display next. Here the controller servlet is the single point of entry, there is a clear sepration of business logic, presentation output and request processing. MCV architecture is a way of decomposing an application into three parts:

Model

  1. The model maintains the state and data that the application represents and uses a Javabean with getters and setters to hold the elements of the form.
  2. The view allows the display of information about the model to the user, where the JSP form retrieves data from the JavaBean.
  3. The controller (which is implemented by means of a Java Servlet) allows the user to manipulate the application the model, the view and the controller.

MVC was originally applied in the graphical user interaction model of input, processing and output.
In Struts, the view is handled by JSPs and presentation components, the model is represented by Java Beans and the controller uses Servlets to perform its action.

A model represents an application’s data and contains the logic for accessing and manipulating that data. Any data that is part of the persistent state of the application should reside in the model objects. The business objects update the application state. The ActionForm bean [1] represents the Model state at a session or request level, and not at a persistent level. Model services are accessed by the controller for either querying or effecting a change in the model state. The model notifies the view when a state change occurs in the model.The JSP file reads information from the ActionForm bean using JSP tags.

View

The view is responsible for rendering the state of the model. The presentation semantics are encapsulated within the view, therefore model data can be adapted for several different kinds of clients.The view modifies itself when a change in the model is communicated to the view. A view forwards user input to the controller.The view is simply a JSP or HTML file. There is no flow logic, no business logic, and no model information just tags. Tags are one of the things that make Struts unique compared to other frameworks like Velocity.

Controller

The controller is responsible for intercepting and translating user input into actions to be performed by the model. The controller is responsible for selecting the next view based on user input and the outcome of model operations.The Controller receives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is a command design pattern implemented as a servlet. The struts-config.xml file configures the Controller.

[1] ActionForm Bean: The Jakarta Struts framework provides the org.apache. struts.action.ActionForm class, which can be used to persist the HTML form data in memory long enough for it to be validated. Once all validation problems are resolved, the data within the ActionForm objects can be used to transfer the form data into the application for processing.