struts-user  

Re: Indexed Property

Mark Lowe
Mon, 22 Mar 2004 05:42:56 -0800

I don't know how helpful those examples are in reality as there are a few details missing..

So here's an example.

Personally i like nesting forms although this isn't to everyone's taste. The example uses LazyList to allow you to scope the form to request and the example was provided by Paul Chip on the list a few weeks back.

// action form

import org.apache.struts.action.ActionMapping;
import javax.servlet.http.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.ActionForm;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.collections.Factory;

public class ItemsForm extends ActionForm {
        
        private List itemList;

        public ItemsForm() {
                initLists();
        }
        
    private void initLists() {
        Factory factory = new Factory() {
            public Object create() {
                return new ProductForm();
            }
        };
        this.itemList = ListUtils.lazyList(new ArrayList(), factory);
    }

        public List getItems() {
                return itemList;
        }

        public void setItems(List itemList) {
                this.itemList = itemList;
        }

        public ItemForm getItem(int index) {
                return (ItemForm) itemList.get(index);
        }
        
        public void setItem(int index,ItemForm item) {
                this.itemList.add(index,item);
        }

    public void reset(ActionMapping mapping,
                      HttpServletRequest request) {
        super.reset(mapping, request);
        initLists();
    }
}

//ItemForm

public class ItemForm extends ActionForm {
        private String name;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
}

//ItemsAction [i'd use LookupDispatchAction]

ItemsForm theForm = (ItemsForm) form;

ItemForm item = new ItemForm();
item.setName("First Item");

ItemForm anotherItem = new ItemForm();
anotherItem.setName("Second Item");

theForm.setItem(0,item);
theForm.setItem(1,item);

// struts config

<form-bean name="itemsForm" type="ItemsForm" />

<action path="/populate" name="itemsForm" scope="request" ...

<action path="/save" name="itemsForm" scope="request"..

//jsp

<html:form path="/save.do">
        <logic:iterate id="item" name="itemsForm" property="items">
                <html:text name="item" property="item" indexed="true" />

or

        <c:forEach var="item" items="${itemsForm.items}">
                <html:text name="item" property="item" indexed="true" />

Sorry I didn't think about the potential confusing of using "item" quite so much.. But this should help close a few gaps.


On 22 Mar 2004, at 14:14, Robert Taylor wrote:


http://jakarta.apache.org/struts/faqs/indexedprops.html

-----Original Message-----
From: Prakasan OK [mailto:[EMAIL PROTECTED]
Sent: Monday, March 22, 2004 6:18 AM
To: Struts Users Mailing List
Subject: Indexed Property


Hi,


can anyone give me a sample code for implementing indexed properties?
how should the jsp page and the corresponding Action class look like?

thanks,
Prakasan

--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]