I present my simple project for a Java web application demonstrating a good use of architecture, programming practices, and the Spring framework.
You can download the NetBeans project here.
Follows a brief description of specs and concepts:
Addressbook
By Khaled A. Hosn
Description and Specifications
A simple address book web application based on the Spring Framework with three separate pages.
The first page should allow the user to input up to 50 names and phone numbers at a time.
The user can input between one and fifty name/numbers at a time.
Assume that there will never be more than 300 names/numbers in total.
Each name must be unique and have only one phone number.
Both the name and the phone number must not be blank.
The names and phone numbers should be stored in memory. No further persistence is required.
Names should be validated to be only letters and blanks.
Phone numbers should be validated to contain only numbers, with an optional + prefix and possibly one pair of brackets with at least one number in them. The phone number must start either with a + or a 0 – if it starts with a +, it cannot be followed by a 0.
These examples are valid phone numbers:
02012345567
+443739182931
+440(03)203739182
These examples are not valid phone numbers:
1322282828 (does not start with a 0 or +)
020-10391-20201 (has non-numeric characters)
+01029818 (starts with + followed by 0)
+44(0)202839(4)3932 – more than one pair of brackets
The second page should list all stored numbers and names, sorted alphabetically.
The third page should allow a user to search the address book by phone number (exact number, not substrings) and also by full name or part of a name (case insensitive). It should display all matching names and related phone numbers for the search criteria.
Overview of the Architecture
The application is divided into the 3 layers of model-view-controller.
A_ The Model consists of 3 sub-layers of Business Objects, Data Access Objects, and an Object Persistence Database (which is itself represented in this case by an Object). These sub-layers exchange data in between themselves and the other tiers using Data Transfer Objects which are formed of lightweight objects only containing properties and accessors. (Moving data through the application by using these transfer objects enforces consistency and makes application code much more readable than passing a large number of arbitrary method parameters).
Within the model, the application has one business object PersonPhoneBO representing a single domain model object. Basically, it has a business logic that relates to ReadWrite functionality for the PersonPhone DTOs (or any of its variants). To do so it uses a DAO which abstracts data access from the business logic tier. This allows at a later date to replace the persistence layer (currently an Object) with a Relational Database and then modify the DAO to use Persistence frameworks such as iBatis or Hibernate.
B_ The View is the presentation layer using Java Server Pages. These pages are purely for presentation purpose, they do not perform any business logic but only presentation logic. They do not have Java Scriplets, but only have tags: JSTL and the Spring-Form Tag Library. The View receives its content from the database through DTOs and from external properties files (for the page titles, instructions, footer). And the pages are styled using CSS.
C_ The Controller is the Web Framework which is handled by the Spring Web MVC module. It is responsible of handling incoming HTTP requests, executing actions on the Business Objects in the Model layer and returning ModelAndView Objects to the View layer. Each returned ModelAndView contains the name of the JSP page to send back to the client and the necessary DTOs which will be used by the tags to display the content.
A request is dispatched by DispatcherServlet to a controller (which is chosen through a handler mapping). Once the controller is finished, the request is then sent to a view (which is chosen through a ViewResolver) to render output.
The application makes use of two types of controllers: AbstractController and SimpleFormController.
-
Write the controller class that performs the logic behind the homepage.
-
Configure the controller in the DispatcherServlet’s context configuration file.
-
Configure a view resolver to tie the controller to the JSP.
-
Write the JSP that will render the homepage to the user.
AbstractController: returns ModelAndView object (you’re required to implement handleRequestInternal). It gives you access to the request/response objects.
SimpleFormController: returns ModelAndView object (where you usually override the OnSubmit method, which gives access to the command object, and the formBackingObject which backs up the page with a command object.
The SimpleFormController let’s you specify a command object, a viewname for the form, a viewname for page you want to show the user when form submission has succeeded, and more.
Khaled A. Hosn, MSc
Internet Engineer