Struts Quick Start: What can it do? How do it do?
This article is selected from the fourth chapter of "Practical J2EE Application Architecture", the author is Nadir Gulzar, and is published by McGraw-Hill / Osborne Media (www.osborne.com) in March 2003
Published on theServerside.com
NOVEMBER 4, 2002
Posted in THSERVERSIDE.COM
November 4, 2002
Introduction
The goal of this article is to introduce the biggest advantage of Struts for expected Struts, and simultaneously illustrate its configuration and method of use. We will define a fully expressing architecture and discuss how to implement these needs in the Struts architecture. We also explore the semantics of the design pattern implemented in Struts, namely the semantics of the controller and related auxiliary components. These knowledge will play a role when designing components that are interacting with the architecture, and this is also necessary to expand the framework to suit a special requirement for a project. This article has complement information provided by http://jakarta.apache.org/struts.
MVC structure
The MVC structure is a method of breaking an application system to three parts (models, views, controllers). Originally applied to graphical user interface (GUI) loss, processing, and output model
Figure
Slightly, see the original book map.
model
The model represents the data of an application system and contains logic to access and manage the data. All data that belong to the persistent status of the application system should be saved in the model of the model. The service provided by the model must be sufficiently adequate to different terminals. Through a list of public methods of roughness, it should be easily understood how to control the behavior of the model. A model aggregates relevant data and operations to provide a detailed and accurate service; these packaging and abstract transaction function in operation is modeled. The interface of a model provides access and updating the model state, performing a method of executing complex processes in the model. The model service is accessed by the controller, used to query or make the status of the model changes. When the status changes, the model will notify the view.
view
The responsibility of the view is the state of the expression model. The presentation statement is packaged in the view, so the model data can be suitable for a variety of different terminals. When the changes in the model are conveyed to the view, the view will modify yourself. The view passes the user input to the controller.
Controller
The task of the controller is to obtain and translate the user input to the action and executed by the model. The task of the controller also selects the next view according to user input and execution results. In an J2EE-based application, the MVC structure is used to separate the character layer functionality represented by JavaBeans or EJB and the performance layer represented by JSP, and the intermediate via the servlet-based controller. However, the controller design must be used for the input of various types of terminals, including HTTP requests from the web terminal, WML, supplier and business partners based on XML-based documentation, and more. For HTTP requests and task paraduses, the submitted HTTP request is sent to a control center, interpreted and delegated to the appropriate request processor. This is also used as a second type of MVC structure. The request processor combines the architecture provided by the developer to implement a request for specific logic associated with the model. Relying on this interaction result, the controller can determine the next view to produce an appropriate reaction. Struts MVC Semantics
We started from the key Struts abstraction, this is the core of its MVC architecture. Struts implements MVC mode using the Service to Worker mode. [Core]
Struts MVC Semantics
We started from the key Struts abstraction, this is the core of its MVC architecture. Struts implements MVC mode using the Service to Worker mode. [Core]
Controller object
The controller is implemented by the ActionServlet class. It provides a central location to process all of the terminal requests. This provides a clearer division for the control layer of the handling view and navigation management, and the model access and operation is left to a dedicated request processor. All submitted requests are mapped to the central processor, and their configuration description is as follows:
servlet>
All requests URI exists with * .do mode and map to this servlet, which is configured as follows:
servlet-maping>
The request URI of this mode meets the following format:
http://www.my_site_name.com/mycontext/Actionname.do
The above mapping is called an extended mapping, you can also declare the path map, using / * ending mode, as shown below:
servlet-maping>
The request URI of this mode meets the following format:
http://www.my_site_name.com/mycontext/do/Action_name
The resource logic map described above allows you to modify the resource mapping in the configuration file without modifying any application system code. This mapping scheme is also used in MULITPLEXED RESOURCE MAPPING. The controller provides a set of access points for all representation layer requests. The controller distinguishes each submitted request to the RequestProcessor, which is turned to send requests to the related form bean for a form authentication, and then issued to a request processor to access the model. The abstract concept provided by this controller reduces the difficulties of developers to build public application system services, such as management views, sessions, and form data. Developers support a general mechanism such as errors and abnormal processing, navigation, internationalization, data verification, data conversion, and more.
In Struts 1.1, the controller needs to read the configuration in the init () method. This configuration controls the behavior of the architecture, contains the Map of the URI to the request processor, such as using an actionMApping configuration object, setting information resources, and provides access to external resources through plugins. In fact, the process of submit request actually occurs in the processServlet commissioning all input requests to RequestProcessor.
Sender object
The RequestProcessor function is to process the request as a sender, with an instantiation (or multiplexed) a request processor and a corresponding form bean to process the request. Form bean and requesting processor's creation or exception throws the RequestProcessor process and affects the view management feature of RequestProcessor. Form Bean Assist RequestProcessor saves form data and / or prepares the intermediate model data necessary for the view. RequestProcessor uses the
Name = "CustomerProfileform" Scope = "request" /> action-mappings> Type = "packagename.customerProfileform" /> All submitted requests are commissioned to the object sender of the RequestProcessor. RequestProcessor Checks the request URI to find actions, and use the information in the ActionMapping object to create a request processor instance, then call the RequestHandler.execute (...) method. Request the execute (...) method task in the processor is to use each other with the application model. The request processor returns to RequestProcessor based on the results. RequestProcessor will use the ActionForward object to call the next view by executing RequestDispatcher.Forward (...) or Response.sendRedirect (...). Use actionmapping command mode Struts provides an open XML-based way to illustrate mappings between the request processor in the request URI and the appropriate request processor. This implementation is very similar to the command mode [GOF]. The following tablets are broken from the struts-config.xml file, the following declarations are used to establish an actionMApping configuration object, which is the runtime performance of the Type = "packagename.editcustomerProfileAction" Name = "CustomerProfileform" Scope = "request" /> action-mappings> The properties used in the above statements are simply described below. PATH: The relative path of the virtual directory in the HTTP request is used to identify this action mapping. TYPE: Class name, will be used to establish a request processor instance when processing this request. Name: The logical name of JavaBean is also called a form bean, which will be used to save the form data. Form Bean will be saved in the specified range (Scope) with this name. Scope: Use the request or session range when you save the bean. In the above example, the Path property is mapped to the For HTML See "Get Form Data" chapters to get more information about Model and the interaction of the request processor A subclass of the Action is an adapter for the request and model of the submitted request. Action subclass, also known as a request processor, is especially built for each request. An action was initially interpreted by the RequestProcessor, and then instantified a corresponding request processor. The object of this Action class is specially established for each request, which is already elaborated in the sender in the previous chapter. The request processor implements the command mode [GOF]. A terminal request is encapsulated in the request URL, the servlet path, which is subsequently extracted by the sender (RequestProcessor) to establish a corresponding request processor instance. Command mode eliminates the impact of the user interface (UI) on the request processor.