The goal of this article is to illustrate how to use
Struts framework and implement some of the J2EE Design Patterns through
a very simple application. This article is in "BETA" version so please
feel free to send me your comments
(even if it's about spelling faults !).
Contents :
- Application presentation
- Class Diagram of the 3-tiers application using Struts and
J2EE Design Patterns
- Sequence Diagram explaining the example
- Download the zip file with source code and the web archive
file (war)
- Links about Struts and J2EE Design Patterns
To print the figures of this article, configure your printer in landscape mode.
back to Top
1. Application presentation |
The example application is composed of 2 pages:
- index.jsp is the welcome-file of the application (the collaboration diagram
below shows that Message 1 populates the Helper Bean which is used in fooInsert.jsp)
- fooInsert.jsp is a simple detail page which allows to add 3 lines of Foo
in the database.

We would like that the user interface shows which lines are not valid by changing
their background colour to red. The screen captures below illustrate this functionality
:
Screen capture of fooInsert.jsp, before the submit
|
This page is composed of a table with 3 lines
of text inputs |
Screen capture of fooInsert.jsp,
after the form is submitted :
 |
The first line and the third line are not
valid. An Error Message has been inserted before the table and the background
colour of invalid lines has been updated to red. |
back to Top

Classes Description
- Presentation Tiers
- JSP1 is the fooInsert.jsp
- GridVO is a bean composed of RowVO
- RowVO encapsulates a String input. The boolean "valid" equals
true if line is valid.
- PrepareAction is called before to go to JSP1. It populates the Helper
Bean used by JSP1 with 3 empty lines.
- AddAction is called when the JSP1's form is submitted to add the inputs
into the database. If the inputs are not valid, it saves an ActionError,
populates the Helper Bean with invalid lines and returns to the input
page.
- Business Tiers
- BusinessDelegate acts as a Proxy between Presentation Tiers and Business
Tiers. It hides the underlying implementation of business services such
as EJB, JNDI calls, databases access ...
- FooVO is a Value Object which encapsulates the business data used by
the BusinessDelegate
- NotValidLinesFunctionalException is an exception thrown by the Business
Logic to indicate that some data are not valid
- Integration Tiers
- DAOFactory is a Factory Method (GoF pattern) which creates DAO (Data
Access Object)
- IFooDAO interface defines a DAO to access Foo table
- MockFooDAO implements the IFooDAO interface. insert method several lines
in a table, it returns a boolean array - true means that the line is valid
and has been inserted.
back to Top
Messages Description
The clients clicks on the link in the welcome page to go to insertFoo.jsp
- 1 The Struts Controller dispatches the Request to PrepareAction which does
the following actions :
- 1.1 instantiates the Helper Bean GridVO and populates it
- 1.2 stores the Bean into the Request and forwards to the JSP
- 1.2.1 The JSP retrieves data from the HelperBean
- 2 The client inputs some datas and submits the form
- 2.1 The Struts Controller dispatches the Request to PrepareAction which
does the following actions :
- 2.1.1 instantiates a Business Delegate and calls add method
- 2.1.1.1 The BusinessDelegate asks the DAO to insert the data
- 2.1.1.1.1 the DAO returns the result of insertion through
a boolean array
- 2.1.1.2 If one or more lines are no valid, the Business Delegate
throws an exception wich contains the invalid lines
- 2.1.2 instantiates the Helper Bean GridVO and populates it
back to Top
The distribution file contains the source code, the javadoc files and the war.
You can download it HERE.
In the build directory, there is a build.xml file. You need to have installed
Ant
1.5 to use it ! Here is a short description of the targets:
- doc - generates javadoc documentation files in the doc directory
- war - compiles the source code and generates the web archive file
- dist - generates a distribution
dist is the default target.
back to Top
Struts:
J2EE Design Patterns :
- View Helper
- Business Delegate
- Value Object
- Data Access Object
Gang of Four Patterns :
Factory Method
back to Top
|
For any comments / suggestions : send an e-mail to g a r n i e r j m @ y a h o o.f r (remove the spaces to get my e-mail, I have done that bc the sobig virus has filled my mailbox!) |
 |