This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Multiple checkboxes and Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Multiple checkboxes and Struts2" Watch "Multiple checkboxes and Struts2" New topic
Author

Multiple checkboxes and Struts2

Casey Cox
Greenhorn

Joined: Nov 28, 2008
Posts: 14
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!
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
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

Joined: Nov 28, 2008
Posts: 14
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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 28, 2008
Posts: 14
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

Joined: Nov 28, 2008
Posts: 14
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 28, 2008
Posts: 14
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

I still think booleans (or maybe *B*ooleans) should work, but glad you got things going
Casey Cox
Greenhorn

Joined: Nov 28, 2008
Posts: 14
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 28, 2008
Posts: 14
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

No sweat
 
Don't get me started about those stupid light bulbs.
 
subject: Multiple checkboxes and Struts2
 
Similar Threads
dynamic checkboxes in struts 2
Getting "not a multipart request" even though the image is being saved.
Problem while receiving parameters..
geting focus on radio button
NullPointerException: Module 'null' not found