Home

 

Struts

The model-view-controller (MVC) architecture pattern is used widely in object-oriented systems as a way of dividing applications into sections with well-defined responsibilities:

Model: Manages the application domain's concepts, both behavior and state. It responds to requests for information about its state and responds to instructions to change its state.

View: Implements the rendering of the model, displaying the results of processing to the uses, and manages user input.

Controller: Receives user input events, determines which action is necessary, and delegates processing to the appropriate model objects.

In dynamic Web applications, the servlet normally fills the role of controller, the JSP fills the role of view and various components, and JavaBeans or Enterprise JavaBeans fill the role of model. The MVC pattern and Struts are is described in in Chapter | 5, Developing Web applications using Struts.

In the context of our banking scenario, this technology does not relate to any change in functionality from the user's point of view. The problem being addressed here is that, although many developers might want to use the MVC pattern, Java EE 5 does not provide a standard way of implementing it. The developers of the RedBank Web application want to design their application according to the MVC pattern, but do not want to have to build everything from the ground up.

Struts was introduced as a way of providing developers with an MVC framework for applications using the Java Web technologies-servlets and JSPs. Complete information about Struts is available at:

http://struts.apache.org/

Struts provides a controller servlet, called ActionServlet, which acts as the entry point for any Struts application. When the ActionServlet receives a request, it uses the URL to determine the requested action and uses an ActionMapping object, created when the application starts up, based on information in an XML file called struts-config.xml. From this ActionMapping object, the Struts ActionServlet determines the action-derived class that is expected to handle the request.

The Action object is then invoked to perform the required processing. This Action object is provided by the developer using Struts to create a Web application and can use any convenient technology for processing the request. The Action object is the route into the model for the application. When processing has been completed, the Action object can indicate what should happen next-the ActionServlet uses this information to select the appropriate response agent (normally a JSP) to generate the dynamic content to be sent back to the user. The JSP represents the view for the application.

Struts provides other features, such as form beans, to represent data entered into HTML forms and JSP tag extensions to facilitate Struts JSP development.

ibm.com/redbooks