Recipe5.3.Using a List-Backed Form Property


Recipe 5.3. Using a List-Backed Form Property

Problem

You want to create and use a form property backed by a java.util.List. You want to be able to access the entire list of data through one method as well as specific values in the list using indexed properties.

Solution

Create the form property as an indexed property, backed by a java.util.List, as shown in Example 5-4.

Example 5-4. List-backed form property
package com.oreilly.strutsckbk.ch05; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public class ListForm extends ActionForm {          private int size = 3;     private List friends = new ArrayList(size);          public List getFriends( ) {          return friends;     }     public String getFriend(int index) {         return (String) friends.get(index);     }     public void setFriend(int index, String name) {         friends.set(index, name);     }        public void reset(ActionMapping mapping, HttpServletRequest request) {         // prepopulate the list with empty strings         friends = new ArrayList( );         for (int i=0; i<size;i++) friends.add("");     } }

Example 5-5 (list_form_test.jsp) shows how the property values can be accessed on a JSP page. The form is created by accessing individual elements as indexed properties. The results are output by iterating over the list.

Example 5-5. Accessing list-backed form properties
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head>     <title>Struts Cookbook - Chapter 5 : List-backed Form Property</title> </head> <body>     <h2>List Form Test</h2>     <html:form action="/ProcessListForm">         Who are your 3 friends:<br />         Friend 1: <html:text property="friend[0]"/><br />         Friend 2: <html:text property="friend[1]"/><br />         Friend 3: <html:text property="friend[2]"/><br />         <html:submit/>     </html:form>     <hr />     <c:forEach var="item" items="${ListForm.friends}">         <c:out value="${item}"/><br />     </c:forEach> </body> </html>

Discussion

Sometimes you need a form property to represent a list of values instead of a single value. The value type may be simple like a String or number, or it may be a complex type composed of other types. An ActionForm can have a property of a simple or complex type that is backed by an implementation of a java.util.List.

So, how do you make a list-backed property accessible from an HTML form? First, you provide getter and setter methods that follow the JavaBean convention for indexed properties:

public Foo getFoo(int index); public void setFoo(int index, Foo foo);

As long as the property follows this convention, it can be accessed regardless of the underlying implementation. If the property values are stored in an implementation of java.util.List, such as a java.util.ArrayList, then the getter method will call list.get(index) and the setter method will call list.set(index, obj).

Though a List, unlike an array, is resizable, you must ensure the getter and setter methods don't attempt to access a value at an index greater than the List size. Doing so will result in an IndexOutOfBoundsException. You can preload the List in the ActionForm's reset( ) method, or you can dynamically resize the list as needed in the getter and setter methods:

public Foo getFoo(int index) {     if (index >= list.size(  )) {         list.add(index, new Foo(  ));     }     return (Foo) list.get(index) } public void setFoo(int index, Foo foo) {     if (index < list.size( )) {         list.set(index, foo);     }     else {         list.add(index, foo);     } }

See Also

Recipe 5.4 discusses how to use Maps for properties in a similar fashion. Recipe 5-5 discusses forms that are even more dynamic; the properties themselves are not determined until runtime.

Check out Recipe 3.4 for details on using multivalue properties on a form.



    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