You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Kevin J. Turner" <kt...@dceg.com> on 2002/02/28 17:32:58 UTC

Multiple checkboxes with the same name -- possible with Struts?

Is it possible, using the Struts framework, to have multiple checkboxes
that are named the same but have different values?
 
First, a little background on this:

Say you have the following four fruity checkboxes on a form, with Apple
and Mango checked:
 
[x] Apple
[ ] Kiwi
[x] Mango
[ ] Banana
 
Each checkbox has the same html *name* property of "fruit", but they all
have different html "value" properties (Apple, Kiwi, etc).
 
In order to get those from the request you would need to do something
like this:
 
String[] fruit = request.getParameterValues("fruit");
 
If you just do a request.getParameter("fruit") with the above scenario,
then you'll only get one string back, "Apple" (because its the first one
selected).
 
So the problem is, where Struts is concerned, is this: Request-scoped
Form beans, behind the scenes, do a request.getParameter() to set the
bean values, correct? But when there are multiple form fields with the
same name, the getParameter() won't do, Struts needs to call
request.getParameterValues(). I'm assuming its not smart enough to do
this? (Or is it doing this and I'm just doing something wrong?)
 
To counter this problem, in my Action classes associated with the form
bean, I grab values from the request object using getParameterValues(),
and store them into a String array. Then I call my setter method,
passing in the array. In other words, I call the setter manually, not
automatically by Struts.
 
Is there another workaround for this or is this my only option?


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Multiple checkboxes with the same name -- possible with Struts?

Posted by "Kevin J. Turner" <kt...@dceg.com>.
I will try again.. there must be something I'm not doing right

Thx.

-----Original Message-----
From: Jonathan James [mailto:jjames@focus-technologies.com] 
Sent: Thursday, February 28, 2002 2:16 PM
To: Struts Users Mailing List; kturner@dceg.com
Subject: Re: Multiple checkboxes with the same name -- possible with
Struts?


>  I'm assuming its not smart enough to do
> this?

Nope. Struts is smart enough to do this. If you use the multibox tag as
David Gaulin
suggested and your bean looks like

public void setFruit( String[] fruits ) {...}
public String[] getFruit() {...}

Then all your multiboxes look like

<html:multibox property="fruit" value="kiwi" />
and so on.

Your bean will be automatically populated with an array of all the
selected
fruits. So in your example

> [x] Apple
> [ ] Kiwi
> [x] Mango
> [ ] Banana

getFruit() in your action would return an array containg the strings
"Apple"
& "Mango"

Are you trying this and it's not working?

-Jonathan


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Multiple checkboxes with the same name -- possible with Struts?

Posted by Jonathan James <jj...@focus-technologies.com>.
>  I'm assuming its not smart enough to do
> this?

Nope. Struts is smart enough to do this. If you use the multibox tag as
David Gaulin
suggested and your bean looks like

public void setFruit( String[] fruits ) {...}
public String[] getFruit() {...}

Then all your multiboxes look like

<html:multibox property="fruit" value="kiwi" />
and so on.

Your bean will be automatically populated with an array of all the selected
fruits. So in your example

> [x] Apple
> [ ] Kiwi
> [x] Mango
> [ ] Banana

getFruit() in your action would return an array containg the strings "Apple"
& "Mango"

Are you trying this and it's not working?

-Jonathan


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>