List based Forms


All along you have seen how to handle regular Forms. Now let us see how to handle list-based forms. List based forms are used for editing collections of objects. Examples include weekly hours-of-operation, contacts etc. Such collections may be limited to a single page or span across multiple pages. We will deal with a collection limited to single page first. Techniques for dealing with multi page lists are illustrated later.

Indexed struts-html tags are used to display editable collections of objects. Consider a HTML form used to collect information about the weekly hours of operation for a company and send the data back to an Action as shown in Figure 6.1. The brute force approach is to create 7 pair of text fields to collect the opening and closing time for each day of the week. An elegant approach is to use indexed < html:... > tags.

The ActionForm for the above HTML in Figure 6.1 is shown in Listing 6.8. The ListForm has a java.util.List named hourOfOperationList . It is a list containing hours of operation. The HourOfOperation itself is a Serializable Java class with three JavaBeans properties ‚ day , openingTime and closingTime . The zeroth day is a Sunday and sixth day is a Saturday. Back to the ListForm . The ListForm has a getter method for the hours of operation List, but no setter method. The reset() method initializes the List with exactly seven HourOfOperation objects. In reality, you would populate this list from database. Also there is an odd method called getTiming() that takes an integer index as argument and returns the corresponding HourOfOperation object from the List. This method replaces the setter method and is the key for the Struts framework when populating the list using form data. The details will become clear once you look at the JSP code in Listing 6.9 and the generated HTML in Listing 6.10.


Figure 6.1: Current and Future page layout for the banking application
Listing 6.8: ListForm
 public class ListForm extends ActionForm {    private List hourOfOperationList;    public ListForm() {         reset();     }     public void reset() {         hourOfOperationList = new ArrayList(7);         hourOfOperationList.add(new HourOfOperation(0));         hourOfOperationList.add(new HourOfOperation(1));         hourOfOperationList.add(new HourOfOperation(2));         hourOfOperationList.add(new HourOfOperation(3));         hourOfOperationList.add(new HourOfOperation(4));         hourOfOperationList.add(new HourOfOperation(5));         hourOfOperationList.add(new HourOfOperation(6));     }     public List getHourOfOperationList() {         return hourOfOperationList;     }     public HourOfOperation getTiming(int index) {         return (HourOfOperation) hourOfOperationList.get(index);     } } 
 
Listing 6.9: JSP for the ListForm
 <html:form action="/submitListForm"> Company Name: <html:text property="companyName" /><BR> <table border=1 cellpadding=1>      <tr><td>Day</td><td>Opening Time</td><td>Closing Time</td></tr>  <logic:iterate  id="timing"  name="ListForm"                                   property="hourOfOperationList">   <tr>     <td><bean:write name="timing" property="dayName"/></td>     <td><html:text name="timing" property="openingTime"  indexed="true"  /></td>     <td><html:text name="timing" property="closingTime"  indexed="true"  /></td>   </tr>  </logic:iterate> </table>  <BR>  <html:submit>Save</html:submit>  <html:cancel>Cancel</html:cancel> </html:form> 
 
Listing 6.10: Generated HTML from JSP in Listing 6.9
 <form name="ListForm" action="/mouse/submitListForm.do"> Company Name:  <input type="text" name="companyName" value="ObjectSource"><BR> <table border=1 cellpadding=1>      <tr><td>Day</td><td>Opening Time</td><td>Closing Time</td></tr>  <tr>     <td>Sunday</td>     <td><input type="text" name="timing[0].openingTime"                                           value="N/A"></td>     <td><input type="text" name="timing[0].closingTime"                                           value="N/A"></td>  </tr> <tr>     <td>Monday</td>     <td><input type="text" name="timing[1].openingTime"                                           value="8:00 AM"></td>     <td><input type="text" name="timing[1].closingTime"                                           value="6:00 PM"></td>  </tr>   .. .. .. </table> <BR><input type="submit" value="Save"> <input type="submit" name="org.apache.struts.taglib.html.CANCEL"                           value="Cancel" onclick="bCancel=true;"> </html:form> 
 

In Listing 6.9, the JSP displays a form containing the company name and the hours of operation List. The < logic:iterate > is used inside the < html:form > tag to iterate over the hoursOfOperationList property in the ListForm bean. Each hour of operation is exposed as a scripting variable named timing . You may be able to relate now between the getTiming() method in the ListForm and this scripting variable. The indexed=true setting on each of the html tags makes the array index to be part of the text field name. For instance, the following tag

 <html:text name="timing" property="openingTime" indexed="true"/> 

generates the HTML as follows in the second iteration ( i=1 ):

 <input type=text name="timing[1].openingTime" .. /> 

Notice the relation between the Struts text tag and the generated input tag. Each text field now has a unique name as the name is partly driven the array index. This magic was done indexed= ‚½true ‚½ setting. When the form is edited and is submitted via POST, the request parameter names are unique ( timing[0].openingTime , timing[1].openingTime etc.), thanks to the array index being part of the text field names. The HTML is shown in Listing 6.10.

Upon form submission, when Struts sees the request parameter named timing[1].openingTime , it calls the following method:

listForm.getTiming(1).setOpeningTime(...)

and so on for every request parameter. This is exactly where the getTiming() method in ListForm comes in handy. Without it, Struts can never access the individual items in the list. Thanks to getTiming() , individual HourOfOperation are accessed and their attributes are set using the corresponding request parameters.

List based form editing is frequently a necessity in day-to-day Struts usage. The above approach is perhaps the only clean way to achieve this.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

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