|
Recipe 5.1. Creating Dynamic Action FormsProblemYou want to create ActionForms for use on your pages without having to handcode a unique Java class for each form. SolutionCreate 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 ActionFormspackage 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"); } } DiscussionStruts 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.
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.
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.
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"/>
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 AlsoRecipe 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. |
|