Sunteți pe pagina 1din 8

Struts 1.

1 Controller Last update :


back to
HOME
UML diagrams
19/12/2003
PAGE

Introduction
The goal of this article is to illustrate the Struts 1.1 Controller with UML diagrams. This
article is an introduction to the Struts framework in order to help beginners
programmers to understand the MVC model 2.

Contents :

1. Context description of the example with the struts-config.xml file


2. Struts 1.1 Controller Class Diagram of the org.apache.struts.action
3. Sequence Diagram
4. Links about Model View Controller model 2 pattern and the Struts
implementation.
5. Use my diagrams - the license to respect if you want to use the code

To print the figures of this article, configure your printer in landscape mode.

back to Top

1. Context description
In order to show how to use the Struts framework classes, I have designed some
diagrams starting from a simple example : a client submits a form to a "forwarded"
page :

struts-config.xml
This is the configuration resource file - which is parsed by the Struts Controller for
initialization.
back to Top

2. Class diagrams
Note
I have forgotten the association between the Action class and the ActionServlet. I will
update the diagram after the summer ...

Application config, org.apache.struts.config package


ModuleConfigImpl contains a collection of static configuration information that
describes an application module. For application initialization, struts-config.xml is
parsed in order to dispatch mapping information in instances of ActionConfig,
ForwardConfig and FormBeanConfig.

• Mapping management :
o ActionConfig contains information of how specific events are mapped to
Actions classes.
 path is a request URI path used to select this mapping. Example
in the jsp page : <form name="myForm" method="post"
action="/user.do">
 type is the Action class name which can handle the event
 name is the name of the form bean, if any, associated with the
action.
 scope is the JSP scope (application, session, request, page) within
which the form bean will be created

Example in struts-config.xml : <action path="/user"


type="org.xxx.UserAction" name="userForm" scope="session">
ActionMapping extends ActionConfig and is not deprecated because of
backward compatibility with version 1.02.

o ForwardConfig encapsulates where the user will be sent to. Properties


name and path correspond to the strut-config.xml line : <forward
name="next" path="/forwardedPage.jsp" />. ActionForward extends
ForwardConfig and is not deprecated because of backward compatibility
with version 1.02.

• ActionForm beans management :


o FormBeanConfig is a definition of ActionForm beans loaded from struts-
config.xml : <form-bean name="userForm"
type="org.apache.struts.action.DynaActionForm" />
o FormPropertConfig is a JavaBean representing the configuration
information of a <form-property> element in struts-config.xml : <form-
property name="property1" type="java.lang.String"/>

The parsing is done by using the famous jakarta Digester, which is a powerful
component of XML - java objects mapping.

Struts Controller
The ActionServlet class is the core of the framework. It is an HTTPServlet which receives
requests from the browser and forwards it depending on a configuration file : struts-
config.xml.

• Since Struts 1.1, the processing logic has been delegated to the
RequestProcessor class :
o Method processMapping selects the ActionConfig used to process the
selection path for this request.
o Method processActionForm retrieves the ActionForm associated with a
mapping or creates and stashes one if necessary.
o Method processPopulate populates the properties of the specified
ActionForm from the request parameters included with this request.
o Method processValidate calls the validate() method of the specified
ActionForm, and forwards back to the input form if there are any errors.
o Method processActionPerform asks the specified Action instance to handle
the request, returns an ActionForward

The subclasses of Action are created and used by RequestProcessor. It is a wrapper


around Business logic. To use Action, subclass it and overwrite the execute() method.

o Method execute processes the HTTP request, and creates the


corresponding HTTP response. Returns an ActionForward instance
describing where and how control should be forwarded.
o Method saveErrors saves the specified error messages keys into the
appropriate request

HTML Form management


• ActionForm represents a general concept of data that is set or updated by a
HTML Form. Struts will automatically set the state of the DynaActionForm using
the corresponding fields from the HttpServletRequest.
• DynaActionForm is Specialized subclass of ActionForm that allows the creation
of form beans with dynamic sets of properties, without requiring the developer
to create a Java class for each type of form bean.
• DynaActionFormClass is used by DynaActionForm to manage its internal state.

Nb : You can still define your own ActionForm in the Struts 1.02 way, by sub-class
ActionForm.

User classes
• HelperBean is a "Helper" class, it is a value object bean which holds the data
required by the screens.
• UserAction is a typical Action, inherits the Struts Action class.
• UserDelegate is based on the Business Delegate pattern. This is an intermediate
class which decouples business components from the code that uses them,
encapsulates business services and access to the Model. See the Struts User's
guide to understand what a Model is in the MVC model 2 pattern.

forwardedPage.jsp is not a class, it is the targeted jsp page. represents the View in the
Model view Controller model 2 pattern.

Error management
• ActionMessages is a collection of ActionMessage
• ActionMessage encapsulates an user message, contains a message key for I18N
management (Internationalization) and 4 objects for parametric replacement in
the message text)
• ActionErrors extends ActionMessages is a collection of ActionError
• ActionError encapsulates error messages

back to Top

3. Sequence Diagram
Messages Description
• 1 the client submits an HTML form, doPost method is called to allow the the
HtppServlet ActionServlet to handle the POST request
• 1.1 The Struts Controller ActionServlet delegates to RequestProcessor the process
of the request
• RequestProcessor does the following actions :
o 1.1.1 retrieve and return the ActionForm bean associated with the
mapping, creating one if necessary
o 1.1.2 populates the ActionForm bean with the input fields of the HTML
form
o 1.1.3 validates the input field values and creates error messages if
validation errors
o 1.1.4 acquires an UserAction instance to process the request, calling the
overwritten execute method of UserAction
 1.1.4.1 retrieves data from the UserActionForm bean by
getProperties methods
 1.1.4.2 calls business services through the BusinessDelegate
 1.1.4.3 populates a value object bean (optional)
 1.1.4.4 forwards to the specified destination in struts-config.xml
• The forwarded page retrieves data from :
o 2 the HelperBean
o 3 and / or from the ActionForm bean

S-ar putea să vă placă și