Recipe5.7.Automatically Creating ActionForms


Recipe 5.7. Automatically Creating ActionForms

Problem

You want to create a DynaActionForm (or subclass) automatically from your business objects without having to configure the form-bean and form-property elements ahead of time.

Solution

Use Hubert Rabago's Formdef plug-in. You can download formdef.jar from https://formdef.dev.java.net/ and copy it to your application's WEB-INF/lib directory. Formdef allows you to define form definitions in an XML file, as shown in Example 5-14.This file should be placed in the WEB-INF folder of your web application and given a meaningful name such as form-defs.xml.

Example 5-14. Formdef form definitions
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE form-definition PUBLIC "-//FormDef//FormDef Form Definition//EN"                                   "form-defs_0_5.dtd"> <form-definition>     <formset>         <form name="EmployeeForm"           beanType="com.oreilly.strutsckbk.ch05.EmployeeFd"/>     </formset> </form-definition>

The form element maps a form named with the name element to a business object whose class is specified by the beanType attribute.

Next, add a plug-in element for Formdef to your struts-config.xml file:

<plug-in className="formdef.plugin.FormDefPlugIn">     <set-property property="defnames"                      value="/WEB-INF/form-defs.xml"/> </plug-in>

The form named in the form element can be used in your application as if you had explicitly declared the form using a form-bean element in the struts-config.xml file. Here's an action that uses the EmployeeFdForm:

<action    path="/SaveEmployeeFd"            name="EmployeeFdForm"           scope="request"            type="com.oreilly.strutsckbk.ch05.SaveEmployeeFdAction">     <forward name="success" path="/view_emp_fd.jsp"/> </action>

Discussion

Formdef was created by Hubert Rabago, who remains the project lead. Formdef is an open-source project licensed under the Apache Software License. At the time of this writing, the latest available version was 0.5.

Formdef maps the business objects of your application's domain model map to your ActionForms. With Struts 1.1, dynamic action forms (see Recipe 5.1) made life easier by allowing you to create an ActionForm declaratively. The Formdef Struts extension takes this notion further by allowing you to create a DynaActionForm behind the scenes that maps to the properties of a business object. An XML file holds the relationship between the form name and the business object.

Foremost, Formdef provides a runtime-created DynaActionForm for a specified business object. Using Formdef, you save yourself the hassle of specifying form-bean and form-property elements in the struts-config.xml file. More importantly, when a new property is added to the business object, you don't have to add a corresponding form-property in the struts-config.xml. The new property will get picked up at runtime and will be available to your form. In this respect, Formdef is similar to capabilities discussed in Recipe 5.5.

Formdef allows you to group together, in the formdefs.xml file, a form definition and its corresponding Struts Validator rules. This feature can make a project more manageable since related data is kept together.

Mapping business objects to web forms is a big affair. Business objects tend to represent data in native formats; forms represent data as strings. A date is represented by the java.util.Date, and numbers are represented by primitives or classes such as java.math.BigDecimal. Business objects can be composed of other business objects. A Person object contains an Address object, and that object, in turn, contains separate properties for the street, city, state, and postal code.

Formdef addresses these issues by allowing you to define how data is converted from a business object to an ActionForm and back again. A Formdef form definition can define and include converters, specialized classes that convert data from one type or format to another.

Formdef includes some utility methods that will transfer data from an ActionForm to your business object applying the specified conversions where needed. These utilities replace the use of the BeanUtils.copyProperties() method. If your business object contains complex types, properties with types other than primitives and Strings. If you want to perform intelligent data transfer between the ActionForm and the business object, you will need to use a converter. Here's one way you could define a converter for a field of type java.util.Date:

<form     name="EmployeeFdForm"       beanType="com.oreilly.strutsckbk.ch05.Employee"/>     <field property="hireDate">         <converter param="mm/DD/yy"/>     </field> </form>

You can localize property conversion by using the key attribute of the converter element. At runtime, the key attribute is used to retrieve the format string from your Struts MessageResources properties file.

<form     name="EmployeeFdForm"       beanType="com.oreilly.strutsckbk.ch05.Employee"/>     <field property="hireDate">         <converter key="format.date.us"/>     </field> </form>

In the MessageResources properties file, you would have an entry like:

format.date.us=mm/DD/yyyy

In this case, the converter uses the default date converter provided with Formdef. The converter element is used to specify the expected format of the date field. You can register your own converter. A converter can be defined as global; it will apply to any property that has the type or name that the converter is defined for. Say you wanted to be able to convert a form input into a phone number object, where the phone number object maintained separate properties for area code, seven-digit number, and extension.

<global-converter for="property-type"         target="com.foo.PhoneNumber"         type="com.foo.util.PhoneNumber "/>

The for="property-type" indicates that the converter is to be applied to all properties of the type specified by the target attribute. Converters can be defined for a target property name.

<global-converter for="converter-name"         target="salary"         param="###,###,##0.00"/>

This converter will be applied to any property named salary, regardless of the property's type.

Keep your eye one the Formdef project. If your application frequently maps business objects to forms on a one-to-one basis, then Formdef may provide a good solution. It allows rapid development by automatically creating ActionForms that remain in synch with your business objects. Formdef is in the early development, so here's your opportunity to contribute!

See Also

At the time of this writing, the Formdef project was being migrated from Hubert Rabago's site (http://www.rabago.net/struts/formdef/) to Java.net (https://formdef.dev.java.net/).

Like Recipe 5.5, this recipe shows ways to reduce the manual effort needed to create ActionForms.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net