February 2005
Intermediate to advanced
528 pages
12h 53m
English
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.
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" ...