Recipe5.5.Lazy Dynamic Action Forms


Recipe 5.5. Lazy Dynamic Action Forms

Problem

You want to create a form where the properties are variable and completely determined at runtime.

Solution

Use Niall Pemberton's Lazy DynaBean forms available for download from http://www.niallp.pwp.blueyonder.co.uk/.

Declare the form-bean to use in the struts-config.xml:

<form-bean name="LazyForm" type="lib.framework.struts.LazyValidatorForm"/>

Then, use the form on a JSP page as you would a normal ActionForm. Example 5-8 shows a JSP page (lazy_form_test.jsp) that will utilize the LazyForm declared previously.

Example 5-8. Using the LazyValidatorForm on an HTML form
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix=   "html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix=   "bean" %> <html> <head>   <title>Struts Cookbook - Chapter 5 : Lazy Form</title> </head> <body>   <h2>Lazy Form Test</h2>   <html:form action="/ProcessLazyForm">     What is your name:<br />       First Name: <html:text property="firstName"/><br />       Last Name: <html:text property="lastName"/><br />     Do you want to subscribe to our newsletter?<br />       <html:checkbox property="subscribe"/><br />     Who are your 3 friends:<br />       Friend 1: <html:text property="friend[0].name"/><br />       Friend 2: <html:text property="friend[1].name"/><br />       Friend 3: <html:text property="friend[2].name"/><br />     <html:submit/>   </html:form>   <hr />   Your name is: <bean:write name="LazyForm" property="firstName"/>&nbsp;                 <bean:write name="LazyForm" property="lastName"/><br />   Your friends are:<br />   <bean:write name="LazyForm" property="friend[0].name"/><br />   <bean:write name="LazyForm" property="friend[1].name"/><br />   <bean:write name="LazyForm" property="friend[2].name"/><br /> </body> </html>

In the Action that receives the form, cast the form into a DynaBean (org.apache.commons.beanutils.DynaBean). You then access the properties using the get("name") method. Since the LazyValidatorForm extends ValidatorForm, you can define standard Struts Validator validation rules in your validation.xml file.

Discussion

Occasionally, you may need to display a group of data input fields that are not known until runtime. Consider an application that allows a potential buyer to configure a product for purchasee.g., the online purchase of a computer. The available configuration options vary based on the product and current promotions; the configuration data actually comes from a database, so the page needs to be built on the fly.

The LazyValidatorForm and the LazyValidatorActionForm allow form properties to be created automatically as they are needed. In particular, indexed properties in these classes are backed by a LazyList. The list will resize as necessary instead of throwing an IndexOutOfBoundsException if you attempt to access an index beyond the current size. Like the DynaActionForm, these forms implement the DynaBean interface and store their properties in a Map. Unlike the DynaActionForm, however, new properties can be added to them at any time. In the Solution, you declare the form in your struts-config.xml file as you would define a custom ActionForm; no form-property elements are specified.

On the JSP page that uses the form, the Struts tags are used as if you were working with a normal ActionForm. In the Solution, two text fields (firstName and lastName), a Boolean field (subscribe), and three values of an indexed property (friend[i].name) are created.

You may not find common uses for these classes; however, they can be helpful in certain scenarios. One such idea is to use these classes when you are prototyping a new application or new feature. Once you nail down the requirements, you can replace the lazy action from with a normal ActionForm or DynaActionForm.

A word of warning: Using the lazy action forms removes the firewall that ActionForms usually provide. In other words, the lazy ActionForm instance will get populated with all the request parameters, whether or not you were expecting them. Make sure that you don't blindly accept the values and pass them onto your business model. Instead, only pull out the specific values that you are expecting.

See Also

Niall Pemberton's web site, http://www.niallp.pwp.blueyonder.co.uk/, has the latest information about these forms as well as some other useful Struts utilities.

Similar capabilities can be achieved using Map-backed form properties. Recipe 5.4 provides more details on this approach.



    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