• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Multiple checkboxes and Struts2

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This must be a fairly common need, but I am not able to figure it out for the life of me.

I have a iterator that displays some rows of data. I have added code to include a checkbox, that the user can select, and then on submission, I do something with those records. But no matter what I try, I am not able to get the checkbox values in the Action class. I must be missing something fundamental!!



I don't have any interceptors defined in my struts.xml.
Please let me know if there is any other information I can provide.

Thanks a LOT for your help!
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to use

String modularCheckBox = request.getParameter("checkBoxName");
if(modularCheckBox!=null && modularCheckBox.equalsIgnoreCase("on")){
// Do your stuff here

}
another scenario
String[] modularCheckBox = request.getParameters("checkBoxName");
 
Casey Cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh! I tried it and got a null.

I was not sure if this is what we need to do, because this is not we how we get at values for textboxes. For struts2, the values get populated automatically in the Action bean, don't they?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you accessed the request directly and still got a null then I'd double-check to make sure the HTML is being rendered appropriately.

If I have time this afternoon I'll do a quick sanity-check--you are correct that you should not have to access the request for checkbox values, but checkboxes (IIRC) can be a little twitchy if using values besides true/false; what are you using as the values?
 
Casey Cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To keep things simple, I removed the multiple checkboxes.

I added a single checkbox to another page of the application. This checkbox, on submission, contains the correct boolean value: true or false, depending on whether the user clicked it or not.

The HTML code is:


I now added the same single checkbox to the page I really want it in, but it does not work. When I checked the source, the HTML rendered is exactly the same. The only difference that I can tell (outside of the difference in content) is, the second page where it does not work, is part of a frame rendered by an action. Can this somehow have a bearing??



The geoProd frame is the one where I want the checkbox to work, but it doesn't.
 
Casey Cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am in the process of learning a valuable lesson today. I was under the impression any invocation of a server side method is equivalent to a submit. But it seems like its not.

If I called a javascript method in the onClick event which just has a document.location.href pointing to an action, the control goes to the server method alright, but the check box values were missing.
But if I call document.frm.submit, it invokes the execute method and the checkbox values were available.

I have to read up more to understand this behavior.
I also have to figure out how to use s:submit - hopefully this will let me properly submit to different methods - sort of like a StrutsDispatchAction without losing any form values.
I also have to figure out how to extend my one checkbox to multiple checkboxes. Whew... Miles to go before I sleep :-(
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct; form submission requires an actual submission, otherwise the browser won't append form parameters to the URL (GET) or put them in the request body (PUT). This is basic HTTP/browser behavior.

<s:submit...> takes a "method" attribute that defines the action method that will be submitted to (as well as "action" and "namespace" attributes that do what their names imply), so that part is pretty easy.

I haven't had a chance to do a quick sanity-check on the checkbox versus array thing yet but hopefully will before the day is done.
 
Casey Cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks much for your help David. But please don't bother with the sanity check for the checkbox. I got the multiple checkboxes to work. I used Strings as values and it works like a charm. No special code change was needed, all I had to do was a proper submit!

Thanks a lot for your time :-)
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think booleans (or maybe *B*ooleans) should work, but glad you got things going
 
Casey Cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Newton:
You are correct; form submission requires an actual submission, otherwise the browser won't append form parameters to the URL (GET) or put them in the request body (PUT). This is basic HTTP/browser behavior.

<s:submit...> takes a "method" attribute that defines the action method that will be submitted to (as well as "action" and "namespace" attributes that do what their names imply), so that part is pretty easy.



Ok, I have changed most of my location.hrefs to s:submits, but there are some instances where I build and append a request parameter.
Most times, these are dynamic values based on what the user chose in a tree or something like that. What would be the best way for me to handle these??

It seems s:submit does not let me embed a s :p aram with a dynamic value.
Using a url href lets me do the parameter part, but I lose the values from my checkboxes.

Is there a way for me to get what I need with s:submit, or should I maybe rethink my need for the request parameters ??

Thanks!
[ December 07, 2008: Message edited by: Casey Cox ]
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd probably just put them in a hidden form field via JavaScript, but w/o knowing what exactly you're doing and how, it's hard to say.
 
Casey Cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Newton:
I'd probably just put them in a hidden form field via JavaScript, but w/o knowing what exactly you're doing and how, it's hard to say.



Aha, that was all I needed. As you can tell, I am pretty new to front-end programming and basic techniques are usually not entirely obvious to me.
This is great, you saved me a lot of heartache. Thanks!
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No sweat
 
reply
    Bookmark Topic Watch Topic
  • New Topic