You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Wendy Smoak <We...@asu.edu> on 2004/01/09 22:46:27 UTC

Using ArrayList instead of String[] properties

I have a String[] property in a DynaValidatorForm.  It works fine with
multiple checkboxes and accepts multiple selections.

Now I need to do an "advanced" user interface, where the user can enter
the information in text boxes and click 'Add'.  Behind the scenes I need
to populate that String[] property (which is named 'accounts').

I don't want to deal with checking the size of the array and the
inevitable out of bounds exceptions, so I thought ArrayList would be a
better choice.  However, when I change to ArrayList, including
struts-config.xml:

  <form-property     name="accounts"      type="java.util.ArrayList"/>

BeanUtils complains when I submit the form that has the multiple
checkboxes:

  org.apache.commons.beanutils.ConversionException: Cannot assign value
of type 
  'java.lang.String' to property 'accounts' of type
'java.util.ArrayList'

The docs indicate that ArrayList is allowed in DynaActionForms (mine is
a DynaValidatorForm):
http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna
_action_form_classes

The multibox tag is:
<c:forEach items="${accountMap[accountForm.map.calendarYear]}"
var="account" >
    <html-el:multibox property="accounts">
       <c:out value="${account.key}"/>
    </html-el:multibox>
    <c:out value="${account.key}"/>&nbsp;
    <c:out value="${account.costCenterDesc}"/>
    <br/>
  </c:forEach>

Which correctly renders checkboxes such as
<input type="checkbox" name="accounts"
value="55_U_ABCD1234">55_U_ABCD1234&nbsp;Some Account<br/>
<input type="checkbox" name="accounts"
value="66_S_EFGH5678">66_S_EFGH5678&nbsp;Some Other Account<br/>

Any idea what I'm doing wrong, or is there a better way to do this?  

-- 
Wendy Smoak
Application Systems Analyst, Sr.
ASU IA Information Resources Management 





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


Re: Using ArrayList instead of String[] properties

Posted by Mark Lowe <ma...@talk21.com>.
Reckon that looks complex to  me  although i tend to use action forms  
not dynaaction forms.

public class Account {
	
	private String name;
	private String number;
	private String description;

	..bla bla

}

may as will use dynaactionform for this example as just an arraylist
<form-bean name="accountsForm">
<form-property name="accounts" type="java.util.ArrayList" />

...
in referring action

DynaActionForm theForm = (DynaActionForm) form;

ArrayList accountList = new ArrayList();
Account account = new Account();
account.setName("My Account");
account.setDescription("Its an account");
account.setNumber("782627367236");

theForm.set("accounts",accountList);

...

<c:forEach var="account" items="${accountsForm.accounts}">
	<c:out value="${account.name}" />
	...

But probably my over simplistic view on such things.


On 9 Jan 2004, at 23:47, Michael McGrady wrote:

> I would personally suggest that you rethink your design.   If you want  
> an "advanced" user interface that does that, then I would use a  
> ListHandler implementation of a ListIterator interface that is so  
> popular with the J2EE pattern people.  The ListIterator interface is  
> something like the following:
>
> package com.michaelmcgrady.util.list;
>
> import java.util.Collection;
> import java.util.List;
>
> import com.michaelmcgrady.exception.ChainedException;
>
> public interface ListIterator {
>   public void       setList(List list) throws ChainedException;
>   public Collection getList();
>   public int        getSize() throws ChainedException;
>   public void       setIndex(int index) throws ChainedException;
>   public int        getCurrentIndex() throws ChainedException;
>   public Object     getCurrentElement() throws ChainedException;
>   public List       getPreviousElements(int count) throws  
> ChainedException;
>   public List       getPreviousElements() throws ChainedException;
>   public List       getNextElements(int count) throws ChainedException;
>   public List       getNextElements() throws ChainedException;
>   public void       resetIndex() throws ChainedException;
> } /// ;-)
>
> This is what I use and it is lightening fast and works the way the  
> design was promised to work.   You can work out what you do with the  
> newer "c" el stuff, but I have done it the following way in code I  
> have not changed:
>
>
>                           <nested:define id='list_handler'  
> type='com.michaelmcgrady.user.ListHandler' scope='session'/>
>                           <nested:root name='list_handler'>
>                             <logic:iterate id='users'  
> name='list_handler' property='list'>
>                             <hr color='<bean:write name="gui_data"  
> property="view.lgnDrkClr" />'>
>                               id:       &nbsp;<bean:write name='users'  
> property='id'/>        <br>
>                               username  &nbsp;<bean:write name='users'  
> property='username'/>  <br>
>                               password  &nbsp;<bean:write name='users'  
> property='password'/>  <br>
>                               name      &nbsp;<bean:write name='users'  
> property='name'/>      <br>
>                               email     &nbsp;<bean:write name='users'  
> property='email'/>     <br>
>                               type      &nbsp;<bean:write name='users'  
> property='type'/>      <br>
>                               status    &nbsp;<bean:write name='users'  
> property='status'/>    <br>
>                               time      &nbsp;<bean:write name='users'  
> property='time'/>      <br>
>                             </logic:iterate>
>                           </nested:root>
>
> Hope this helps.  Don't know anything about dynamically uploading  
> applets, do you?  I cannot seem to get that right.
>
> Regards,
>
> Michael McGrady
> At 01:46 PM 1/9/2004, you wrote:
>
>> I have a String[] property in a DynaValidatorForm.  It works fine with
>> multiple checkboxes and accepts multiple selections.
>>
>> Now I need to do an "advanced" user interface, where the user can  
>> enter
>> the information in text boxes and click 'Add'.  Behind the scenes I  
>> need
>> to populate that String[] property (which is named 'accounts').
>>
>> I don't want to deal with checking the size of the array and the
>> inevitable out of bounds exceptions, so I thought ArrayList would be a
>> better choice.  However, when I change to ArrayList, including
>> struts-config.xml:
>>
>>   <form-property     name="accounts"      type="java.util.ArrayList"/>
>>
>> BeanUtils complains when I submit the form that has the multiple
>> checkboxes:
>>
>>   org.apache.commons.beanutils.ConversionException: Cannot assign  
>> value
>> of type
>>   'java.lang.String' to property 'accounts' of type
>> 'java.util.ArrayList'
>>
>> The docs indicate that ArrayList is allowed in DynaActionForms (mine  
>> is
>> a DynaValidatorForm):
>> http://jakarta.apache.org/struts/userGuide/ 
>> building_controller.html#dyna
>> _action_form_classes
>>
>> The multibox tag is:
>> <c:forEach items="${accountMap[accountForm.map.calendarYear]}"
>> var="account" >
>>     <html-el:multibox property="accounts">
>>        <c:out value="${account.key}"/>
>>     </html-el:multibox>
>>     <c:out value="${account.key}"/>&nbsp;
>>     <c:out value="${account.costCenterDesc}"/>
>>     <br/>
>>   </c:forEach>
>>
>> Which correctly renders checkboxes such as
>> <input type="checkbox" name="accounts"
>> value="55_U_ABCD1234">55_U_ABCD1234&nbsp;Some Account<br/>
>> <input type="checkbox" name="accounts"
>> value="66_S_EFGH5678">66_S_EFGH5678&nbsp;Some Other Account<br/>
>>
>> Any idea what I'm doing wrong, or is there a better way to do this?
>>
>> --
>> Wendy Smoak
>> Application Systems Analyst, Sr.
>> ASU IA Information Resources Management
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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


Re: Using ArrayList instead of String[] properties

Posted by Michael McGrady <mi...@michaelmcgrady.com>.
I would personally suggest that you rethink your design.   If you want an 
"advanced" user interface that does that, then I would use a ListHandler 
implementation of a ListIterator interface that is so popular with the J2EE 
pattern people.  The ListIterator interface is something like the following:

package com.michaelmcgrady.util.list;

import java.util.Collection;
import java.util.List;

import com.michaelmcgrady.exception.ChainedException;

public interface ListIterator {
   public void       setList(List list) throws ChainedException;
   public Collection getList();
   public int        getSize() throws ChainedException;
   public void       setIndex(int index) throws ChainedException;
   public int        getCurrentIndex() throws ChainedException;
   public Object     getCurrentElement() throws ChainedException;
   public List       getPreviousElements(int count) throws ChainedException;
   public List       getPreviousElements() throws ChainedException;
   public List       getNextElements(int count) throws ChainedException;
   public List       getNextElements() throws ChainedException;
   public void       resetIndex() throws ChainedException;
} /// ;-)

This is what I use and it is lightening fast and works the way the design 
was promised to work.   You can work out what you do with the newer "c" el 
stuff, but I have done it the following way in code I have not changed:


                           <nested:define id='list_handler' 
type='com.michaelmcgrady.user.ListHandler' scope='session'/>
                           <nested:root name='list_handler'>
                             <logic:iterate id='users' name='list_handler' 
property='list'>
                             <hr color='<bean:write name="gui_data" 
property="view.lgnDrkClr" />'>
                               id:       &nbsp;<bean:write name='users' 
property='id'/>        <br>
                               username  &nbsp;<bean:write name='users' 
property='username'/>  <br>
                               password  &nbsp;<bean:write name='users' 
property='password'/>  <br>
                               name      &nbsp;<bean:write name='users' 
property='name'/>      <br>
                               email     &nbsp;<bean:write name='users' 
property='email'/>     <br>
                               type      &nbsp;<bean:write name='users' 
property='type'/>      <br>
                               status    &nbsp;<bean:write name='users' 
property='status'/>    <br>
                               time      &nbsp;<bean:write name='users' 
property='time'/>      <br>
                             </logic:iterate>
                           </nested:root>

Hope this helps.  Don't know anything about dynamically uploading applets, 
do you?  I cannot seem to get that right.

Regards,

Michael McGrady
At 01:46 PM 1/9/2004, you wrote:

>I have a String[] property in a DynaValidatorForm.  It works fine with
>multiple checkboxes and accepts multiple selections.
>
>Now I need to do an "advanced" user interface, where the user can enter
>the information in text boxes and click 'Add'.  Behind the scenes I need
>to populate that String[] property (which is named 'accounts').
>
>I don't want to deal with checking the size of the array and the
>inevitable out of bounds exceptions, so I thought ArrayList would be a
>better choice.  However, when I change to ArrayList, including
>struts-config.xml:
>
>   <form-property     name="accounts"      type="java.util.ArrayList"/>
>
>BeanUtils complains when I submit the form that has the multiple
>checkboxes:
>
>   org.apache.commons.beanutils.ConversionException: Cannot assign value
>of type
>   'java.lang.String' to property 'accounts' of type
>'java.util.ArrayList'
>
>The docs indicate that ArrayList is allowed in DynaActionForms (mine is
>a DynaValidatorForm):
>http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna
>_action_form_classes
>
>The multibox tag is:
><c:forEach items="${accountMap[accountForm.map.calendarYear]}"
>var="account" >
>     <html-el:multibox property="accounts">
>        <c:out value="${account.key}"/>
>     </html-el:multibox>
>     <c:out value="${account.key}"/>&nbsp;
>     <c:out value="${account.costCenterDesc}"/>
>     <br/>
>   </c:forEach>
>
>Which correctly renders checkboxes such as
><input type="checkbox" name="accounts"
>value="55_U_ABCD1234">55_U_ABCD1234&nbsp;Some Account<br/>
><input type="checkbox" name="accounts"
>value="66_S_EFGH5678">66_S_EFGH5678&nbsp;Some Other Account<br/>
>
>Any idea what I'm doing wrong, or is there a better way to do this?
>
>--
>Wendy Smoak
>Application Systems Analyst, Sr.
>ASU IA Information Resources Management
>
>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org

Re: Using ArrayList instead of String[] properties

Posted by Mark Lowe <ma...@talk21.com>.
I think your multibox needs to be index="true" or be a checkbox with  
index="true".

On 9 Jan 2004, at 21:46, Wendy Smoak wrote:

>
> I have a String[] property in a DynaValidatorForm.  It works fine with
> multiple checkboxes and accepts multiple selections.
>
> Now I need to do an "advanced" user interface, where the user can enter
> the information in text boxes and click 'Add'.  Behind the scenes I  
> need
> to populate that String[] property (which is named 'accounts').
>
> I don't want to deal with checking the size of the array and the
> inevitable out of bounds exceptions, so I thought ArrayList would be a
> better choice.  However, when I change to ArrayList, including
> struts-config.xml:
>
>   <form-property     name="accounts"      type="java.util.ArrayList"/>
>
> BeanUtils complains when I submit the form that has the multiple
> checkboxes:
>
>   org.apache.commons.beanutils.ConversionException: Cannot assign value
> of type
>   'java.lang.String' to property 'accounts' of type
> 'java.util.ArrayList'
>
> The docs indicate that ArrayList is allowed in DynaActionForms (mine is
> a DynaValidatorForm):
> http://jakarta.apache.org/struts/userGuide/ 
> building_controller.html#dyna
> _action_form_classes
>
> The multibox tag is:
> <c:forEach items="${accountMap[accountForm.map.calendarYear]}"
> var="account" >
>     <html-el:multibox property="accounts">
>        <c:out value="${account.key}"/>
>     </html-el:multibox>
>     <c:out value="${account.key}"/>&nbsp;
>     <c:out value="${account.costCenterDesc}"/>
>     <br/>
>   </c:forEach>
>
> Which correctly renders checkboxes such as
> <input type="checkbox" name="accounts"
> value="55_U_ABCD1234">55_U_ABCD1234&nbsp;Some Account<br/>
> <input type="checkbox" name="accounts"
> value="66_S_EFGH5678">66_S_EFGH5678&nbsp;Some Other Account<br/>
>
> Any idea what I'm doing wrong, or is there a better way to do this?
>
> -- 
> Wendy Smoak
> Application Systems Analyst, Sr.
> ASU IA Information Resources Management
>
>
>
>
>
> ---------------------------------------------------------------------
> 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