Web DbFormsby Joachim Peer
Many developers find themselves writing similar JSP and servlet code again and again when creating Web-based database applications. The open source project DbForms provides a solution that reduces the amount of coding to an absolute minimum.
This article gives an overview of DbForms and its concepts and features, and shows some code examples.
DbForms is a Java-based Rapid Application Development (RAD) environment which enables developers to build Web-based database applications in a very short time and with very little effort. DbForms applications are built in a conceptually very similar manner to RAD database-building tools like Microsoft Access (for Windows-based applications) or Sybase PowerSite (for Web-based applications). The basic principle of these RAD-Tools could be described as "placing database-aware components and action elements on templates (forms) which get executed at runtime."
The project's homepage is located at http://www.dbforms.org and contains a complete user manual, several technical articles, source and binary distributions, online examples, a CVS, mailing lists, and lots of other information related to DbForms.
The MVC Paradigm Incorporated by DbForms
DbForms implements the concepts of the Model-View-Controller (MVC) design pattern. (See Design Patterns: Elements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison Wesley, October 1994.) You do not need to provide any Controller-related code; just focus on defining the Model and creating the JSP view components of the application (mainly using DbForms custom tags).
The following discusses how Model, View and Controller interact in DbForms.
The Model: Database Objects Described by Database Metadata
The use of DbForms is to perform operations on databases. The database tables accessed by a DbForms application must be declared in a XML configuration file (usually stored as
WEB-INF/dbforms-config.xml), which is parsed and evaluated at Web-application startup time.
Listing 1. Defining the model
<field name="id" fieldType="int" isKey="true" />
<field name="firstname" fieldType="char" />
<field name="lastname" fieldType="char" />
<field name="address" fieldType="char" />
<field name="orderid" fieldType="int" isKey="true" />
<field name="customerid" fieldType="int" isKey="true" autoInc="true" />
<field name="date" fieldType="char" />
<field name="annotation" fieldType="char" />
<field name="amount" fieldType="int" />
name = "jdbc/dbformstest"
isJndi = "true"
As shown in Listing 1, every table (or view) to be accessed by DbForms has to be declared inside of a
<table> element. All relevant table fields need to be declared inside of a
<field> element nested within its respective
There exists a tool for generating this XML data automatically. The tool reads database metadata of a specified database and constructs a configuration file, as shown in Listing 1.
The Controller: Parsing, Dispatching, and Executing Events
As pointed out above, you do not need to provide any Controller-related code when the Controller is a true infrastructural component. However, it's useful if you have an idea of what's going on inside the Controller. The following lines provide that information.
The Controller includes several components:
ControllerServlet: this servlet is the single point of entry for all incoming HTTP requests from clients.
EventEngine: an assistant to the
ControllerServlet, it focuses on filtering HTTP requests for
WebEvents and instantiates them.
WebEventObjects: all Objects derived from the abstract super-class
WebEventhave the ability to initialize themselves by reading a given HTTP request. These events get executed, either directly by the Controller or by the View.
The following description of the execution of a typical user action should give you a better picture of what the controller does and how it interacts with the other components:
- User presses the button "delete row" on his/her DbForms application.
- Client browser submits data via
ControllerServletdelegates the incoming request to the
EventEngine, which determines the main event (the user explicitly triggered the event by clicking a button). However, there may occur implicit events, too -- i.e., automatic updating of all changed input fields of all data rows.
EventEnginecomponent parses that request and determines the kind of action the user wants to be executed.
- It then creates the appropriate
WebEvent(in our case, a
DeleteEvent) and delegates the request Object to this newly-created
WebEvent, which finalizes its own initialization. After that, the
EventEnginereturns the recently-created and -initialized event back to the
ControllerServlettells the event to execute its built-in operation, if it is a
DatabaseEvent. Other events (
NavigationEvent, etc.) are delegated to the appropriate View component.
EventEngineagain to check if there are additional (implicit) events to be executed. If so, the appropriated
WebEventObjects are created and executed in the same manner as the main event described above.
ControllerServletdetermines the View component to which the request should be forwarded. If found, the
ControllerServletinvokes the component and forwards the request.
- If the View component is a JSP page containing DbForms tags, those tags will search for navigation events to be executed, and will finally generate the response for the user.
- The response is rendered by the user's Web browser.
The View: JSP Templates Provided by the Application Developer
The View portion of a DbForms application is generally constructed using JSP technology. JSP files may contain static HTML elements, as well as dynamic elements containing Java code (definitions, statements, expressions). For more information about JSP, please see Sun's JSP page.
With release 1.1 of the JSP API, a powerful facility called JSP tag libraries was added. With these custom tags, you can encapsulate even most sophisticated Java code into lightweight JSP tags.
DbForms makes use of the great potential of JSP tag libraries. It contains an extensive custom tags library for rendering and manipulating database data.
The Structure of a DbForms View
Figure 1 gives a conceptual overview of the main components of a typical DbForms view.
The Basic Concepts of Forms
Each DbForms-view JSP may have one or more root tags of the type
form tag has to contain exactly one
header tag, exactly one
body tag, and exactly one
footer tag, in exactly that order.
Each of those tags may contain sub-elements like data fields, input fields, action buttons, and -- of course -- plain HTML text and JSP code.
footer tags are commonly used for titles of pages, labelling tables, placing action and navigation buttons, input fields to enter new data, etc.
footer tags get evaluated only once.
body tag is used for showing data rows coming from the database, and for providing the user with the functionality to edit that data. How many times the body tag and its sub-elements get rendered depends on the value of the
maxRows attribute of the form element (and of course, on the number of datasets actually fetched from the database).
maxRows = n-- body gets executed
ntimes at maximum (with n in N)
maxRows = "*"-- body gets executed for every row in the table ("endless" form)
Every form may contain one or more nested sub-forms inside its
The "orders" form is nested within the
body element of the "customer" form, as shown in Figure 2. The user will see one customer per page (because
maxRows is set to "1") and all the orders (because
maxRows = "*") the customer has taken. The user may navigate through the list of customers by clicking the navigation buttons.
OK, Let's See Some Code!
As stated before, JSP views are the only parts of a DbForms application a developer usually needs to put his/her hands on. DbForms provides an extensive custom tag library which makes this an easy task, so that it can be performed even by non-programmers.
As we will see later, much of this code can be generated automatically by tools included in DbForms. However, it is useful to understand the basics of DbForms views, even if much of the work can be done automatically.
The following two sections will show a simple and a more advanced example of a DbForms view. Both examples can be thought as being parts of a little CRM application of a (virtual) agency.