I didn't realize that Struts could actually pass a pre-filled ArrayList.
That's pretty cool.    The way that I've done index properties in Struts
call this method in my version:

Myproperty(int index, String value);

In that method, you can recreate whatever you want however you prefer.
This is a good reference for you to use -->
http://struts.apache.org/faqs/indexedprops.html

You also can use regular struts html tags to create the indexed values
for you by using the indexed=true property instead of hand-writing out
the name values as in the example below.  I use indexed properties to
recreate lists as well as objects in lists by tricking struts into doing
the right thing so the below will work.  It may just be that I have not
explained it clearly if it doesn't work for you.

Hope it helps you.

Regards...djsuarez

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 30, 2004 2:35 PM
To: [EMAIL PROTECTED]
Subject: Re: How to handle multiploe unknown form fields

In fact it did help because it answered some question for me.  I spent
the last hour searching for the RIGHT answer... This looked like it, but
for whatever reason it would never work for me when I tried to do the
exact same thing in my own project.  So, I went ahead and hacked
together my own solution...

In my test JSP, I have the following:

<form name="test" method="post" action="test.mtx">
  <input type="text" name="skills[0]" value="val0">
  <input type="text" name="skills[1]" value="val1">
  <input type="text" name="skills[2]" value="val2">
  <input type="submit" name="submit" value="submit">
</form>

Then in my ActionForm, I have:

private ArrayList skills = new ArrayList();
public ArrayList getSkills() {
  this.skills.add(new String(""));
  return this.skills;
}
public void setSkills(ArrayList skills) {
  this.skills = skills;
}
public ArrayList getSkillsClean() {
  for (Iterator it = this.skills.iterator(); it.hasNext();) {
    String s = (String)it.next();
    if (s.trim().equalsIgnoreCase("")) {
      it.remove();
    }
  }
  return this.skills;
}

Struts knows that it's an indexed property and knows how to populate the
ActionForm.  The problem I found is that you either have to (a) have an
initial capacity for the ArrayList and more importantly you must
initialize all the elements because the getSkills() method is called for
each element that is added.  So, instead, I add an element in
getSkills() myself.  The problem is, if you then later call getSkills()
from the Action, as one would expect to do, you'll always have an empty
element at the end (or more, if you happen to call the method more than
once).  No big deal, but I decided I didn't like it, so I added the
getSkillsClean() method, which removes the empty elements.

I don't think I'm doing this the right way, and indeed the link you sent
shows a more elegant solution, but as I said it wouldn't work for me
when I tried, and I like things that work (I'm odd that way!), and this
has that virtue, so I'm happy.

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com

On Thu, September 30, 2004 2:01 pm, [EMAIL PROTECTED] said:
> 
> 
> 
> 
> 
> Frank,
> 
> Will this help?  http://www.reumann.net/struts/nested.do
> 
> You didn't say anything about JSTL.  Just today I'm working on
populating
> a
> checkbox field in an object that's contained in a list.  Oops.  I just
> remembered I'm using html-el too.  I haven't tried it with with plain
> html.
> This is getting it done tho'.
> 
> c:forEach items="${workQueueForm.workQueueList}" var="workQueue"
> varStatus=
> "status">
> 
>                tr >
>                   td class="Data_AlignMiddle">
>                         html-el:checkbox property=
> "workQueueList[${status.index}].checked" />
> 
>                   /td>
> 
> btw - how do you guys get code in your email w/o it messing up the
> archives?
> 
> 
> 
> 
> 
> 
>              [EMAIL PROTECTED]
>              om
>
To
>              09/30/2004 01:54          [EMAIL PROTECTED]
>              PM
cc
> 
>
Subject
>              Please respond to         Re: How to handle multiploe
unknown
>                "Struts Users           form fields
>                Mailing List"
>              <[EMAIL PROTECTED]
>                   he.org>
> 
> 
> 
> 
> 
> 
> 
> I understand the JSP side of this eqation as you wrote it, although I
> should have said I was looking for a solution that doesn't use Struts
> taglibs because I try to avoid them at all costs, but that aside...
> 
> I'm still unclear however on what the ActionForm does... Using this
> concept, do I HAVE to use the LazyActionForm you wrote?  I'd prefer to
> only
> use things that are built-in to Struts, and unless I'm missing it in
the
> docs, that's not.
> 
> The question I'm getting at is that, like I said, the JSP code you
wrote
> makes sense, but what will put the submitted parameters into the
> collection
> in the ActionForm when the submission happens?  That's the part I
don't
> see.  Thanks for your help!
> 
> --
> Frank W. Zammetti
> Founder and Chief Software Architect
> Omnytex Technologies
> http://www.omnytex.com
> 
> On Thu, September 30, 2004 1:51 pm, Niall Pemberton said:
>> You simply need a property in your ActionForm that returns a
collection
> of
>> "skill" beans and used the "indexed" attribute on the <html> tags.
The
>> "isssue" that most people have problems with is when using a
"Request"
>> scope
>> ActionForm you need to populate your collection with the right number
of
>> skill beans - the way to handle this is some kind of "lazy list"
>> processing
>> for that property. Search the archives on indexed properties and lazy
> list
>> processing.
>>
>> In your jsp...
>>
>> <logic:iterate name="skillsForm" property="skills" id="skills">
>>    <html:text name="skills" property="skillid" indexed="true"/>
>>    <html:select name="skills" property="skillLevel" indexed="true">
>>         <html:option value="1">Low</html:option>
>>         <html:option value="2">Medium</html:option>
>>         <html:option value="3">High</html:option>
>>    </html:select>
>> </logic:iterate>
>>
>> The trick is to name the "id" attribute to the same as the property
in
> the
>> form which returns the collection, that way Struts will generate
> something
>> like:
>>
>>  <input type="text" name="skills[x].skillid value=".."/>
>>
>>
>> The lazy ActionForms I wrote have the lazy list behaviour built
in....
>>
>> http://www.niallp.pwp.blueyonder.co.uk/#lazydynabean
>>
>> Niall
>>
>>
>> ----- Original Message -----
>> From: <[EMAIL PROTECTED]>
>> To: <[EMAIL PROTECTED]>
>> Sent: Thursday, September 30, 2004 6:19 PM
>> Subject: How to handle multiploe unknown form fields
>>
>>
>>> I have an interesting situation, one that has never come up before,
and
>> I'm unsure how to deal with it...
>>>
>>> Imagine you have some records from a database representing various
>>> skills
>> (i.e., HTML, Javascript, J2EE, etc.).  Each has a SkillID associated
>> with
>> it.
>>>
>>> You create a JSP that lists each skill with a drop-down next to it. 
>>> The
>> drop-down allows the user to select their skill level for each skill.
>>>
>>> When the user hits Save, you need to update all the skills for that
>>> user.
>>>
>>> That's the scenario.  Here's the question... Each drop-down is given
>>> the
>> name of the SkllID.  But how do you write an ActionForm for that?
>>>
>>> Since the database can be expanded to include new skills at any
time,
>>> it's
>> impractical to add getters and setters for each SkillID, and in fact
>> breaks
>> low coupling goals anyway.
>>>
>>> Is there a standard way of accepting what kind of amounts to an
array
>>> of
>> inputs from a form and getting it into an ActionForm in some way
(maybe
> as
>> an ArrayList or something?).
>>>
>>> TIA!
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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]
> 
> 
> ---------------------------------------------------------------------
> 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]

Reply via email to