Struts
Thursday, November 1, 2007 4:35:10 AM
One of the best things about Struts is that it addresses many of the negative issues that come along with a MVC framework. Struts is a reliable and pre-tested MVC framework. Also, Struts is well documented, resulting in a reduced learning curve. Unlike the complicated task of building your own MVC system, the most complicated part of Struts is just reading and understanding the excellent documentation that comes with the product. OK, if you are a new JSP programmer, learning how to use a MVC system can also be a challenge.
Notes on the process flow:
web.xml Surprise! The first step is not the browser request, rather it is the setup. One of the key secrets to Struts is the configuration files. The first thing that has to happen, occurs when you start your JSP container. The JSP container will check the web.xml configuration file and determine what Strut action Servlets exist. The JSP container will then map all appropriate file requests to go to the correct action Servlet. For example, it is common to map any request for a file that ends with the .do extension to be processed by the default Struts Action Servlet!
A Request The second step happens when a user requests a page or submits a form within a browser. This request is then intercepted by the controller.
The Controller This is the central processor within Struts. There can be one or several controllers. Most Struts applications will only have one ActionServlet (the controller), which directs several Actions. Within Struts the controller is a Servlet. The controller determines what action is required and sends the information to be processed by an action Bean. The controller could also send the request straight to a JSP page to display back to the browser. A key advantage of having a controller is the ability to control the flow of logic through highly controlled, centralized points. This is perfect for performing security and filtering logic. In JSP 1.2 some of the need for this goes away with the filtering and event processing logic. However, for JSP 1.1, this centralized controller is one of the best ways to perform logic that is required by all pages.
struts-config.xml To help make life more modular, Struts has a configuration file to store mappings of actions. The advantage of this system is the prevention of hard coding the module to be called within a component. This means it is easy to swap around different actions by merely changing a single initialization file. The controller will check the struts-config.xml file to determine which modules to call upon an action request. This makes the struts-config.xml file the central heart of Struts. Struts only reads the struts-config.xml upon start up. It creates a mappings database (a listing of the relationships between objects) which is stored in memory to speed up performance.
Model The model is an object to take the response from the user and store the results for the duration of the process. Often the model object will take all of the data from the form being submitted and store the results in a JavaBean to pass along as the process continues. This is a great place to perform preprocessing of the data received from the request. It is conceivable that one model object will exist for each request page. It is also possible to reuse one model object for many page requests. Sometimes a process will not require a model object and the controller could send the request straight to the view object. Since model objects are project specific, a programmer must create the model objects. Struts provides the ActionForm and Action classes which can be extended to create the model objects.
View The view object often is a JSP page geared for producing output to the user. Since Struts is a framework which relies on code existing in a predetermined manner, it has many Tag libraries to make building the view page even easier for a programmer.
Strut Tag Libraries The Struts Tag libraries are Strut components to allow the easy integration of the Struts framework within the project's logic. These Tag libraries hide most of the work of using Struts behind simple-to-use Tag statements. These Tags are only used within the view components. The reason for this is that Tag libraries only work within a JSP page. This means the "Controller" Servlet and "Model" JavaBeans cannot use a Tag library and instead must use the Struts class library for Strut process control. Our the diagram shows the Tag Libraries directly accessing the model, in a more complete sense they actually access the model objects indirectly through JavaBeans.
Property File The property file is a way to store messages that an object or page can use. Often the Struts Tag libraries will also use the property files for titles and other string data. The advantage being you can create many different property files to handle different languages. Also, the property files can act like skins for your messages, allowing a you to easily change messages from project to project.
Business Objects The business objects are where the rules of the actual project live. These are the modules which drive the day-to-day site activities. Struts merely controls the process flow of the site, it is not the project. The project still consists of the JSP pages that the user will see and the business objects that drive the logic of the project. Since Struts spreads out the logical flow of a project among many steps, this means it is possible to call business objects from any of the steps.
The Response This is the final page the user will see. Often this is simply the output of the View JSP object.
more
http://www.jspinsider.com/content/jsp/struts/strutsadvance.jsp













