ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Jakarta Struts: Seven Lessons from the Trenches
Pages: 1, 2, 3, 4

6. Use Dynamic Forms

In the Struts framework, ActionForm objects are used to capture HTML form data sent along with the request and also to provide data that can be used to help render the dynamic views. They are essentially JavaBean classes that extend from the Struts ActionForm class and have the option of overriding a couple of default methods.

This feature is a big time saver, as it helps to facilitate automatic presentation-layer validation. The only downside to ActionForms has been the need to create multiple ActionForm classes to hold the data for the various HTML forms. For example, if you have one page for the details of a user registration and another page for user preferences, you typically would need two separate ActionForm classes. One of the nicest features of Struts 1.1 is the notion of dynamic ActionForms.

Dynamic ActionForms, which are implemented by the DynaActionForm class and various subclasses, allow you to configure an ActionForm completely through the Struts configuration file; there's no longer a real need to create actual concrete ActionForm classes in your application. In the Struts configuration file, you can configure a DynaActionForm for your application by adding a <form-bean> element and setting the type to be the fully-qualified class name of the DynaActionForm or one of its subclasses. The following example creates a dynamic ActionForm called logonForm that contains two instance variables: username and password.

  <form-property name="username" type="java.lang.String"/>
  <form-property name="password" type="java.lang.String"/>

This dynamic ActionForm can be used within Action classes and JSPs in the same way that a regular ActionForm is used, with one small difference. With normal ActionForm objects, we would provide get and set methods to get and set the data. In the example above, we would have to provide a getUsername() and setUsername() method and also a set for the password variable.

Because we are using a DynaActionForm (which stores the variables in a Map), we need to use the get(name) and set(name) method on the DynaActionForm class, where the argument is the name of the instance variable we are trying to access. For example, if we wanted to access the username value from a DynaActionForm, the code would look like this:

String username = (String)form.get("username");

Since the values are stored in a Map, we must cast the Object returned from the get() method call.

There are several useful subclasses of the DynaActionForm. The most important of these is arguably the DynaValidatorForm. This dynamic ActionForm is used with the Validator to provide automatic validation using the common Validator package. This is another nice feature that allows you to specify validation rules outside of the application source. Combining these two features is a very attractive incentive for developers.

7. Use Visual Tools

Since the release of 1.0, there have been several Visual tools created to help create, modify and maintain the Struts configuration file. The configuration file itself is based on XML and for any medium to large-sized application, can grow to be unwieldy. To better manage this file once it has grown beyond what is considered easy-on-the-eyes, you might try using one of the GUI tools developed for this purpose. There are several commercial and open source tools available. Table 1 lists several of the tools available and URLs where you can find out more information.

Table 1. Struts GUI tools

Name Cost URL
Adalon Commerical http://www.synthis.com/products/adalon
Easy Struts Open Source http://easystruts.sourceforge.net/
Struts Console Free http://www.jamesholmes.com/struts/console
JForms Commerical http://www.solanasoft.com/
Camino Commerical http://www.scioworks.com/scioworks_camino.html
Struts Builder Open Source http://sourceforge.net/projects/rivernorth/
StrutsGUI Free http://www.alien-factory.co.uk/struts/struts-index.html


For a more complete list of Struts GUIs (both free and commercial), visit the Struts resource page.

Chuck Cavaness is a graduate from Georgia Tech with degrees in computer engineering and computer science. He has built Java-based enterprise systems in the healthcare, banking, and B2B sectors. He is also the author of two O'Reilly books, Programming Jakarta Struts and Jakarta Struts Pocket Reference.

O'Reilly & Associates will soon release (November 2002) Programming Jakarta Struts.

Return to ONJava.com.