Recipe5.1.Creating Dynamic Action Forms


Recipe 5.1. Creating Dynamic Action Forms

Problem

You want to create ActionForms for use on your pages without having to handcode a unique Java class for each form.

Solution

Create a form-bean in the struts-config.xml file using the built-in DynaActionForm type or any of its subclasses. Then define the properties of the form using nested form-property elements:

<form-beans>   <form-bean name="MyForm" type="org.apache.struts.action.DynaActionForm">     <form-property name="foo" type="java.lang.String"/>     <form-property name="bar" type="java.lang.String"/>     <form-property name="baz" type="java.lang.Boolean"/>     <form-property name="blindMice" type="java.lang.String[]"                    size="3"/>   </form-bean> <form-beans>

You can retrieve the data from the form using methods of the DynaActionForm or generically using the Jakarta Commons PropertyUtils class. The Action shown in Example 5-1 uses this technique to get the form property values by name.

Example 5-1. Using PropertyUtils with ActionForms
package com.oreilly.strutsckbk.ch05; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class ProcessAction extends Action {   public ActionForward execute(ActionMapping mapping,       ActionForm form,       HttpServletRequest request,       HttpServletResponse response) throws Exception {              String foo =          (String) PropertyUtils.getSimpleProperty(form, "foo");     String bar =          (String) PropertyUtils.getSimpleProperty(form, "bar");     Boolean baz =          (Boolean) PropertyUtils.getSimpleProperty(form, "baz");     String[] mice =          (String[]) PropertyUtils.getProperty(form, "blindMice");     return mapping.findForward("success");   } }

Discussion

Struts 1.1 introduced the ability to create ActionForms without writing any Java code; you create the ActionForm declaratively in your struts-config.xml file. This feature simplifies the application and reduces the amount of Java source code that you have to write and maintain. Prior to Struts 1.1, you could only create a custom ActionForm by extending the Struts base class, org.apache.struts.action.ActionForm. Your custom class consisted of getter and setter methods for the form properties and validation for the property values. Then along came the Struts Validator. Developers no longer needed to code the validate( ) method; they could provide form validation using an XML-based configuration file. Once the validation was removed from the custom ActionForm, the class became a group of trivial getter and setter methods.

Struts 1.1 relieves the burden of writing these trivial classes through the use of dynamic action forms. The dynamic action form is implemented by the org.apache.struts.action.DynaActionForm class. This class implements the DynaBean interface provided by the Jakarta Commons BeanUtils package. The interface provides methods that allow a class to contain a dynamic set of properties, much like a java.util.Map. The main benefit of a DynaBean over a simple map is that its values can be accessed just like standard JavaBean properties using the BeanUtils introspection utilities.

If you aren't using the Validator or you have to code a custom validate( ) method, you lose the benefit of the DynaActionForm and you might as well code a conventional ActionForm subclass.


You create a dynamic action form in the struts-config.xml file by specifying the value for the type attribute of the form-bean element as the fully qualified class name of the DynaActionFormorg.apache.struts.action.DynaActionFormor one of its subclasses. The form-property elements specify the name and type of the properties that make up the form. While DynaActionForms officially support a number of different types, you should stick with Strings and Booleans.

Booleansthat is, true/false valuesare commonly used for checkboxes.


DynaActionForms do support other types, such as java.lang.Long and java.sql.Date. However, these types can cause problems with form validation and should be avoided in all action forms.

Use Strings for Form Properties

Form property types that aren't Strings or Booleans are problematic because the ActionForm is populated prior to being validated.

If a user enters alphabetic characters into a field corresponding to a property of a numeric typee.g., java.lang.Integer--then the form can't be populated and a runtime exception will be thrown by the BeanUtils.populate( ) method.

But if you use String properties, then the form can be populated successfully regardless of the input. Once populated, form validation, implemented in the validate( ) method, can check the data for correctness.

If the data pass validation, then the values can be converted as needed and passed on to other business objects. If not, control returns to the input page where the values can be redisplayed and the user can correct them.


The DynaActionForm allows a property to be defined as a scalar, or single value, or as an array of values by using the [] notation following the type value. In the Solution, the form property blindMice is defined as an array of Strings with an array size of three elements:

<form-property name="blindMice"                 type="java.lang.String[]"                size="3"/>

The size attribute applies only to array properties.


When it comes to retrieving the values from the DynaActionForm, you can use the API of the DynaActionForm class or use the property accessor utility classes provided by the Jakarta Commons BeanUtils package.

See Also

Recipe 5.2 describes how to set initial values for form properties. Recipe 5.6 goes into greater detail about accessing the values from action forms.

Complete details on how the Jakarta Commons BeanUtils utility classes work can be found in the JavaDoc package description at http://jakarta.apache.org/commons/beanutils/api/index.html.



    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