aspose file tools*
The moose likes Struts and the fly likes How to get checkboxes from request parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to get checkboxes from request parameter" Watch "How to get checkboxes from request parameter" New topic
Author

How to get checkboxes from request parameter

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Hi,

I am using LazyValidatorForm in my action class. I am using mapped property for checkbox. Lets say i have a question with checkboxes on my form
Example:
Q)What do you own? .car .van .truck
This question has a unique ID which is question1 and the checkboxes are question values qv1, qv2 and qv3
If you check one checkbox car then i get question1{qv1} in the request parameters. i am saving car checkbox to db after saving, when the user comes back and unchecks car checkbox i need to delete it from the db but the problem is if he unchecks the checkbox i am not getting any thing in the request parameters but i expect it to be question1{null}.

Thanks,
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

That's how checkboxes work (atleast in IE). You select a checkbox option, the corresponding name=value is included in the request. When its not selected, the 'name' is absent in the query string.

ram.
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
ram,

Thanks for your reply but, if i want to delete the checkbox if it is unchecked how would we do it??
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's one way to do it:

When the form is submitted, query the database again to get the old values and put them in another instance of your ActionForm. Then do something like this:


This will work only if ActionForm is in request scope. If it is in request scope, when the form is submitted, a new instance of your Actionform will be created and get{"question1") will still return null even though no "question1" parameter was sent from the form.
[ May 03, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
One thing I have done for similar operations is to store the initial values as hidden fields on the JSP. Then you can compare the settings to determine if you need to delete records.

- Brent
Vijendra Runwal
Ranch Hand

Joined: Apr 17, 2006
Posts: 52
I had same problem sometime back.
I copied the array(representing checkboxes) to another hidden array in java script and passed it to action class and that's working fine.
i used the above method for updating my db.
i think this will work for deletion also.
Hope u got the solution.



SCJP 1.4,SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get checkboxes from request parameter
 
Similar Threads
saving multiple rows
jsp self submit
Problem with selected checkbox value in struts 1.2
Submitting only selected textboxes in a JSP
dynamic Checkboxe value retrieval