You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Tony Karas <to...@hotmail.com> on 2001/05/09 18:49:40 UTC

Posting Collections

Briefly, this is what I am trying to achieve:

- Retrieve a list of items from a database
- Display each item with a corresponding checkbox
- Display a "Delete" button - which when pressed deletes all checked items.

Unfortunately, although I have managed to display the items correctly and 
set the checkbox value using boolean values, when I do the form submit - my 
ActionForm properties do not get filled in.

This is the code I have:

My ActionForm looks like this:

public class RetailerForm extends ActionForm
{
    protected Vector retailer;

    /*
     * On construction, fill the form with all the retailers
     */
    public RetailerForm() throws SQLException
    {
         //here i have some code to generate my vector
         //which is comprised of "Retailer" beans.
    }

    public Retailer getRetailer( int index )
    {
        return (Retailer)retailer.elementAt( index );
    }

    public Vector getRetailer()
    {
        return retailer;
    }

    public void setRetailer( Vector value )
    {
        retailer = value;
    }
}

My "Retailer" bean has get and set elements for properties called "delete" 
and "name".

My struts code looks like this (obviously within <html:form> tags):

<logic:iterate id="retailer" name="retailerForm" property="retailer">
	<tr>
	<td><html:checkbox name="retailer" property="delete"/></td>
	<td><bean:write name="retailer" property="name"/></td>
	</tr>
</logic:iterate>

And this all works ok for displaying the data.  However, when I do the 
submit my "delete" property for each bean is not set and I have added 
debugging code to the "set" method and this is not called by struts.  I 
don't get error messages - it just doesn't happen.

I know other people have had this problem, but I am struggling to find a 
solution.

Can anyone help me?  Is there a better way of achieving what I am trying to 
achieve?  If I am doing the correct thing, what's the reason it's not 
working.

All help appreciated.

Cheers
Tony
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


RE: Posting Collections

Posted by Niall Pemberton <ni...@btInternet.com>.
The problem is the CheckBox tag currently sets the HTML name attribute to
the property, so your JSP will produced something like this:

        <input type="checkbox" name="delete">

So all the checkbox fields will have the same name.

In order for Struts to populate your Retailer bean with the delete values
you need the generated HTML to look like this:

        <input type="checkbox" name="retailer[0].delete">
        <input type="checkbox" name="retailer[1].delete"> etc etc.

Struts would then use getRetailer(0).setDelete(boolean) to set the delete
attributes correctly.

Unfortunately there is no easy solution - many people resort to scriptlets
to generate the name properly and there are quite a few messages in the
archive about this.

I created my own versions of Struts tags and changed them to generate the
name correctly - I like that much better - no java in the view.

Niall

> -----Original Message-----
> From: Tony Karas [mailto:tonykarastomcatuser@hotmail.com]
> Sent: 09 May 2001 17:50
> To: struts-user@jakarta.apache.org
> Subject: Posting Collections
>
>
> Briefly, this is what I am trying to achieve:
>
> - Retrieve a list of items from a database
> - Display each item with a corresponding checkbox
> - Display a "Delete" button - which when pressed deletes all
> checked items.
>
> Unfortunately, although I have managed to display the items correctly and
> set the checkbox value using boolean values, when I do the form
> submit - my
> ActionForm properties do not get filled in.
>
> This is the code I have:
>
> My ActionForm looks like this:
>
> public class RetailerForm extends ActionForm
> {
>     protected Vector retailer;
>
>     /*
>      * On construction, fill the form with all the retailers
>      */
>     public RetailerForm() throws SQLException
>     {
>          //here i have some code to generate my vector
>          //which is comprised of "Retailer" beans.
>     }
>
>     public Retailer getRetailer( int index )
>     {
>         return (Retailer)retailer.elementAt( index );
>     }
>
>     public Vector getRetailer()
>     {
>         return retailer;
>     }
>
>     public void setRetailer( Vector value )
>     {
>         retailer = value;
>     }
> }
>
> My "Retailer" bean has get and set elements for properties called
> "delete"
> and "name".
>
> My struts code looks like this (obviously within <html:form> tags):
>
> <logic:iterate id="retailer" name="retailerForm" property="retailer">
> 	<tr>
> 	<td><html:checkbox name="retailer" property="delete"/></td>
> 	<td><bean:write name="retailer" property="name"/></td>
> 	</tr>
> </logic:iterate>
>
> And this all works ok for displaying the data.  However, when I do the
> submit my "delete" property for each bean is not set and I have added
> debugging code to the "set" method and this is not called by struts.  I
> don't get error messages - it just doesn't happen.
>
> I know other people have had this problem, but I am struggling to find a
> solution.
>
> Can anyone help me?  Is there a better way of achieving what I am
> trying to
> achieve?  If I am doing the correct thing, what's the reason it's not
> working.
>
> All help appreciated.
>
> Cheers
> Tony
> _________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
>
>


RE: Posting Collections

Posted by Niall Pemberton <ni...@btInternet.com>.
The problem is the CheckBox tag currently sets the HTML name attribute to
the property, so your JSP will produced something like this:

        <input type="checkbox" name="delete">

So all the checkbox fields will have the same name.

In order for Struts to populate your Retailer bean with the delete values
you need the generated HTML to look like this:

        <input type="checkbox" name="retailer[0].delete">
        <input type="checkbox" name="retailer[1].delete"> etc etc.

Struts would then use getRetailer(0).setDelete(boolean) to set the delete
attributes correctly.

Unfortunately there is no easy solution - many people resort to scriptlets
to generate the name properly and there are quite a few messages in the
archive about this.

I created my own versions of Struts tags and changed them to generate the
name correctly - I like that much better - no java in the view.

Niall

> -----Original Message-----
> From: Tony Karas [mailto:tonykarastomcatuser@hotmail.com]
> Sent: 09 May 2001 17:50
> To: struts-user@jakarta.apache.org
> Subject: Posting Collections
>
>
> Briefly, this is what I am trying to achieve:
>
> - Retrieve a list of items from a database
> - Display each item with a corresponding checkbox
> - Display a "Delete" button - which when pressed deletes all
> checked items.
>
> Unfortunately, although I have managed to display the items correctly and
> set the checkbox value using boolean values, when I do the form
> submit - my
> ActionForm properties do not get filled in.
>
> This is the code I have:
>
> My ActionForm looks like this:
>
> public class RetailerForm extends ActionForm
> {
>     protected Vector retailer;
>
>     /*
>      * On construction, fill the form with all the retailers
>      */
>     public RetailerForm() throws SQLException
>     {
>          //here i have some code to generate my vector
>          //which is comprised of "Retailer" beans.
>     }
>
>     public Retailer getRetailer( int index )
>     {
>         return (Retailer)retailer.elementAt( index );
>     }
>
>     public Vector getRetailer()
>     {
>         return retailer;
>     }
>
>     public void setRetailer( Vector value )
>     {
>         retailer = value;
>     }
> }
>
> My "Retailer" bean has get and set elements for properties called
> "delete"
> and "name".
>
> My struts code looks like this (obviously within <html:form> tags):
>
> <logic:iterate id="retailer" name="retailerForm" property="retailer">
> 	<tr>
> 	<td><html:checkbox name="retailer" property="delete"/></td>
> 	<td><bean:write name="retailer" property="name"/></td>
> 	</tr>
> </logic:iterate>
>
> And this all works ok for displaying the data.  However, when I do the
> submit my "delete" property for each bean is not set and I have added
> debugging code to the "set" method and this is not called by struts.  I
> don't get error messages - it just doesn't happen.
>
> I know other people have had this problem, but I am struggling to find a
> solution.
>
> Can anyone help me?  Is there a better way of achieving what I am
> trying to
> achieve?  If I am doing the correct thing, what's the reason it's not
> working.
>
> All help appreciated.
>
> Cheers
> Tony
> _________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
>
>


RE: Posting Collections

Posted by "Deadman, Hal" <ha...@tallan.com>.
I think I am doing the same thing that you want to do using the
html:multibox. When the form is submitted you get an array of the ids that
are checked and then you can go delete them.

in the jsp form (where restaurant id is a key for the item being deleted):
<logic:iterate id="restaurant" name="favoriterestaurants">
...
	<html:multibox property="favoriteRestaurantRemoveList">
		<bean:write property="restaurantId" name="restaurant"/>
	</html:multibox>
...
</logic:iterate>


in the form class:
    private Long favoriteRestaurantRemoveList[] = new Long[0];

    public Long[] getFavoriteRestaurantRemoveList() {
        return (this.favoriteRestaurantRemoveList);
    }

    public void setFavoriteRestaurantRemoveList(Long
favoriteRestaurantRemoveList[]) {
        this.favoriteRestaurantRemoveList = favoriteRestaurantRemoveList;
    }

I am not sure if initializing the array to new Long[0] is necessary,
probably not.

> -----Original Message-----
> From: Tony Karas [mailto:tonykarastomcatuser@hotmail.com]
> Sent: Wednesday, May 09, 2001 12:50 PM
> To: struts-user@jakarta.apache.org
> Subject: Posting Collections
>
>
> Briefly, this is what I am trying to achieve:
>
> - Retrieve a list of items from a database
> - Display each item with a corresponding checkbox
> - Display a "Delete" button - which when pressed deletes all
> checked items.
>
> Unfortunately, although I have managed to display the items
> correctly and
> set the checkbox value using boolean values, when I do the
> form submit - my
> ActionForm properties do not get filled in.
>
> This is the code I have:
>
> My ActionForm looks like this:
>
> public class RetailerForm extends ActionForm
> {
>     protected Vector retailer;
>
>     /*
>      * On construction, fill the form with all the retailers
>      */
>     public RetailerForm() throws SQLException
>     {
>          //here i have some code to generate my vector
>          //which is comprised of "Retailer" beans.
>     }
>
>     public Retailer getRetailer( int index )
>     {
>         return (Retailer)retailer.elementAt( index );
>     }
>
>     public Vector getRetailer()
>     {
>         return retailer;
>     }
>
>     public void setRetailer( Vector value )
>     {
>         retailer = value;
>     }
> }
>
> My "Retailer" bean has get and set elements for properties
> called "delete"
> and "name".
>
> My struts code looks like this (obviously within <html:form> tags):
>
> <logic:iterate id="retailer" name="retailerForm" property="retailer">
> 	<tr>
> 	<td><html:checkbox name="retailer" property="delete"/></td>
> 	<td><bean:write name="retailer" property="name"/></td>
> 	</tr>
> </logic:iterate>
>
> And this all works ok for displaying the data.  However, when
> I do the
> submit my "delete" property for each bean is not set and I have added
> debugging code to the "set" method and this is not called by
> struts.  I
> don't get error messages - it just doesn't happen.
>
> I know other people have had this problem, but I am
> struggling to find a
> solution.
>
> Can anyone help me?  Is there a better way of achieving what
> I am trying to
> achieve?  If I am doing the correct thing, what's the reason it's not
> working.
>
> All help appreciated.
>
> Cheers
> Tony
> ______________________________________________________________
> ___________
> Get Your Private, Free E-mail from MSN Hotmail at
http://www.hotmail.com.