You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Torsten Römer <to...@luniks.net> on 2007/05/23 12:13:05 UTC

Struts2 Dynamic Checkboxes

I guess this is a quite common issue but I seem to only find
solutions/examples for Struts1:

I have a varying number of checkboxes on a form and would like to
somehow get a list of the selected checkboxes or a map and of course it
would be nice if the checkboxes would "remember" their state...

Any suggestions welcome!

Torsten

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


Re: Struts2 Dynamic Checkboxes

Posted by Torsten Römer <to...@luniks.net>.
Ah, I am such a dingbat... the beans in my list contained a property
with the same name as I used for the Collection property in the backing
bean. So when the tag was checking if the option was there or not it was
looking at some boolean property of that bean instead into the Collection.

Not before I downloaded the Struts2 source and debugged the
ListUIBean.compare() method I realized that ;-)

So this answers my question, and I am happy to say that my dynamic
checkboxes now work perfectly!

The Dingbat

Torsten Römer schrieb:
> I think I found an OK solution but one thing does not work and I don't
> understand why.
> 
> So what I am doing is creating single checkboxes in an iterator,
> disabling the automatically generated label through a modified "simple"
> theme and creating my custom labels:
> 										<s:iterator value="cars">
>   <s:label>
>     <s:checkboxlist templateDir="template"
>                     template="checkboxlist.ftl"
>                     theme="nolabel"
>                     name="car"
>                     list="%{''+name}"/>						    <s:text
> name="format.moneyround"><s:param value="price"/></s:text>
>     <s:property value="name"/>							  </s:label>									</s:iterator>
> 
> "cars" is a List of simple beans with, among other, a "name" property.
> The "car" property is a Collection. When I check a checkbox and submit
> the form, I can see that an XWorkList containing the value is passed to
> setCar().
> BUT: When the form is redisplayed, as because of a validation error, the
> getCar() method is not called and the checkbox is not checked.
> 
> Can anyone tell me why?
> 
> If I move the <s:checkboxlist> tag outside the <s:iterator>, it works,
> but like this I can't create my custom labels, which is what this is all
> about...
> 
> Torsten
> 
> Torsten Römer schrieb:
>> Well, why not try both ways... it will be a good exercise no matter what.
>>
>> I'll get back when I have some interesting experiences to share with.
>>
>> Thanks again!
>>
>> Torsten
>>
>> Dave Newton schrieb:
>>> --- Torsten Römer <to...@luniks.net> wrote:
>>>> I see there is no point in what I am trying to do.
>>> Oh, I don't know about that.
>>>
>>> You could use a map (or list) for the checkbox values
>>> and gather up the result values that way, couldn't
>>> you? 
>>>
>>> You just need to pre-populate the map (or list) in the
>>> action before hitting the view.
>>>
>>> d.
>>>
>>>
>>>
>>>        
>>> ____________________________________________________________________________________Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
>>> http://answers.yahoo.com/dir/?link=list&sid=396545469
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

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


Re: Struts2 Dynamic Checkboxes

Posted by Torsten Römer <to...@luniks.net>.
I think I found an OK solution but one thing does not work and I don't
understand why.

So what I am doing is creating single checkboxes in an iterator,
disabling the automatically generated label through a modified "simple"
theme and creating my custom labels:
										<s:iterator value="cars">
  <s:label>
    <s:checkboxlist templateDir="template"
                    template="checkboxlist.ftl"
                    theme="nolabel"
                    name="car"
                    list="%{''+name}"/>						    <s:text
name="format.moneyround"><s:param value="price"/></s:text>
    <s:property value="name"/>							  </s:label>									</s:iterator>

"cars" is a List of simple beans with, among other, a "name" property.
The "car" property is a Collection. When I check a checkbox and submit
the form, I can see that an XWorkList containing the value is passed to
setCar().
BUT: When the form is redisplayed, as because of a validation error, the
getCar() method is not called and the checkbox is not checked.

Can anyone tell me why?

If I move the <s:checkboxlist> tag outside the <s:iterator>, it works,
but like this I can't create my custom labels, which is what this is all
about...

Torsten

Torsten Römer schrieb:
> Well, why not try both ways... it will be a good exercise no matter what.
> 
> I'll get back when I have some interesting experiences to share with.
> 
> Thanks again!
> 
> Torsten
> 
> Dave Newton schrieb:
>> --- Torsten Römer <to...@luniks.net> wrote:
>>> I see there is no point in what I am trying to do.
>> Oh, I don't know about that.
>>
>> You could use a map (or list) for the checkbox values
>> and gather up the result values that way, couldn't
>> you? 
>>
>> You just need to pre-populate the map (or list) in the
>> action before hitting the view.
>>
>> d.
>>
>>
>>
>>        
>> ____________________________________________________________________________________Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
>> http://answers.yahoo.com/dir/?link=list&sid=396545469
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

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


Re: Struts2 Dynamic Checkboxes

Posted by Torsten Römer <to...@luniks.net>.
Well, why not try both ways... it will be a good exercise no matter what.

I'll get back when I have some interesting experiences to share with.

Thanks again!

Torsten

Dave Newton schrieb:
> --- Torsten Römer <to...@luniks.net> wrote:
>> I see there is no point in what I am trying to do.
> 
> Oh, I don't know about that.
> 
> You could use a map (or list) for the checkbox values
> and gather up the result values that way, couldn't
> you? 
> 
> You just need to pre-populate the map (or list) in the
> action before hitting the view.
> 
> d.
> 
> 
> 
>        
> ____________________________________________________________________________________Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
> http://answers.yahoo.com/dir/?link=list&sid=396545469
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

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


Re: Struts2 Dynamic Checkboxes

Posted by Dave Newton <ne...@yahoo.com>.
--- Torsten Römer <to...@luniks.net> wrote:
> I see there is no point in what I am trying to do.

Oh, I don't know about that.

You could use a map (or list) for the checkbox values
and gather up the result values that way, couldn't
you? 

You just need to pre-populate the map (or list) in the
action before hitting the view.

d.



       
____________________________________________________________________________________Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469

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


Re: Struts2 Dynamic Checkboxes

Posted by Torsten Römer <to...@luniks.net>.
It sure does and I already looked at it, but I was trying to create
single checkboxes in an <s:iterator> in order to be able to put them in
a custom layout.

I kind of have the same problem with radiobuttons so I guess I should
reconsider my "design" so I can use the default markup and thus the tags
as they are supposed to be used...

I see there is no point in what I am trying to do.

Thanks!

Torsten

Dave Newton wrote:
> --- Torsten Römer <to...@luniks.net> wrote:
>> I have a varying number of checkboxes on a form and
>> would like to somehow get a list of the selected 
>> checkboxes or a map and of course it would be nice
> if
>> the checkboxes would "remember" their state...
> 
> http://struts.apache.org/2.x/docs/checkboxlist.html
> 
> Does that meet your needs?
> 
> d.
> 
> 
> 
>        
> ____________________________________________________________________________________Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online.
> http://smallbusiness.yahoo.com/webhosting 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

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


Re: Struts2 Dynamic Checkboxes

Posted by Dave Newton <ne...@yahoo.com>.
--- Torsten Römer <to...@luniks.net> wrote:
> I have a varying number of checkboxes on a form and
> would like to somehow get a list of the selected 
> checkboxes or a map and of course it would be nice
if
> the checkboxes would "remember" their state...

http://struts.apache.org/2.x/docs/checkboxlist.html

Does that meet your needs?

d.



       
____________________________________________________________________________________Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online.
http://smallbusiness.yahoo.com/webhosting 

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