struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Thompson <michael.r.thomp...@sun.com>
Subject Re: Best Practice, Variable Number Of Form Elements
Date Tue, 08 Jul 2003 16:03:02 GMT
If you have indexed props with lists of java objects
on your form:

public Person getPerson(int index);
public void setPerson(int index, Person person);
public Collection getPersons();

and you have a <html:text> or whatever
<html:text property="person[4].name"/>

then struts(beanutils really?) will do something like

yourFormInstance.getPerson(4).setName(theValue);

What I've done in the past is lazily create person objects in the 
getPerson method

private ArrayList _persons = new ArrayList();

public Person getPerson(int index)
{
    while (index < _people.size())
        _people.add(new Person());

    return _people.get(index);
}

There are other methods for doing this lazy loading.  I'm pretty sure 
I've seen a LazyLoadingList or something in the jakarta commons projects.

    --m


James Childers wrote:

>>The PROBLEM is submitting this form. I haven't found a way to 
>>make Struts
>>auto-allocate space in a collection (or a Map, for that matter) in an
>>ActionForm for the submit action. Almost every example on 
>>this I've seen
>>deals with a fixed number of elements, and most of these 
>>still focus on the
>>pre-populate action. These examples completely avoid this issue.
>>
>>So, when the number of elements on the form VARIES, what is the "best
>>practice" in design for this? I'm talking about an indexed or mapped
>>collection that contains other Java Beans. How can you get these to 
>>submit properly into an ActionForm? If there are options, can you weigh 
>>the pros/cons of each?
>>
>>For example, I'm sure a session-scope bean would accomplish 
>>this, but I stay away from session-scope beans because of clustered 
>>deployment issues.
>>    
>>
>
>Make your Form elements String[] objects. These will automatically be populated (and sized)
when the form is submitted. Since the ServletRequest interface works with attributes using
Strings, this is just about the only way to do things. You can't pass Java objects across
HTTP, at least not using GET or POST. You're forced to deal with strings by the protocol.
>
>If you need to use something other than array, another solution is needed. Once your Form
is passed to your Action, you can get the number of elements you will need using request.getParameterValues(String).
This method returns a String[]; you could get the length of this array and use it to size
your collection object as needed.
>
>As an aside, I think you may be worrying about storing things in session more than is
warranted. Modern containers are very good at tracking session information across clusters.
We have a cluster of over a dozen systems for our site, and we have never encountered any
problems with storing data in session. We try to minimize the use of sessions for the sake
of efficiency and design, not because of problems with data being replicated across different
systems.
>
>
>-= J
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Mime
View raw message