Solved

Getting 'unchecked' value from Struts checkbox

Posted on 2004-08-23
10
1,209 Views
Last Modified: 2007-12-19
I know that checkbox is only supposed to return a value IF checked but I'm getting a checked value returned all the time.  I'm using a DynaActionForm on a form that does a search i.e. you fill in 2 boxes at the top, click Search and the results are dsplayed below i.e. I can't just re-display the form nicely reset to default values.

When I uncheck the checkbox I expect there to be no reference to it in the dynaactionfom, yet there it is with a value of On!  Which means I cannot check for it correctly in the Action class.

How can I stop this please?

Thanks, Simon
0
Comment
Question by:ssherlock
  • 3
  • 3
10 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11871189
If you use a multibox like suggested here:

http://www.jguru.com/faq/view.jsp?EID=925277

then it returns an array of string that DOES match up to what was clicked...ie:  if it isn't selected, it isn't in the array...

Hope this helps...  checkboxes are a pain...  If this doesn't work, I should be able to find another way of doing it in my code somewhere ;-)

Tim
0
 

Author Comment

by:ssherlock
ID: 11871288
It seems overkill to try and use multibox for one field (and the article still mentions calling the reset method).  Do I need to look at somehow over riding the form (bearing in mind I'm using a DynaActionForm) that has a reset() method?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11871358
As you say, try resetting the checkbox value to false in the reset method of the form...

Hmmm..  I can't for the life of me remember how I fixed this...sorry...I'll have to dig back through my code...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Accepted Solution

by:
ssherlock earned 0 total points
ID: 11872861
It's so easy it's embarrassing!
Simply add a hidden field immediately after the checkbox set to false i.e.
<html:checkbox property="currentLocation" value="true"/>
<html:hidden name="currentLocation" value="false">

Struts (or maybe forms in general) reads the first line only.  If the checkbox is unchecked then the hidden field gets rid, otherwise the first one gets read.


Works a treat!
0
 

Author Comment

by:ssherlock
ID: 11872904
Of course it helps if you copy and paste the hidden field so the typos don't creep in :)
<html:hidden property="currentLocation" value="false"/>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11872927
;-)  Cool :-)

Glad you got it sorted :-)

(I must remeber to bookmark this page for next time I come up against that problem) ;-)

Hehehe

Tim
0
 
LVL 3

Expert Comment

by:Gunt
ID: 11876116
Although it works, be careful with that solution, it sounds fragile to me (two inputs with same name, only the first get processed, who knows if it's always that way, and if it's going to change in the future).
I suggest you check the code and identify how is it only the first found field with a name is being used.
Just an opinion to add, since you already figured it out.

Another solution (a little more verbose, but safer), is to create your dynaform class. Is very easy, you create a class and extend from DynaActionForm. I've done it to add lots of functionallity to the dynaform and works great (in that custom class you can then reset before submit or whatever you want to do).

Good luck.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTTPSessionBindingListernter not configured in deployment descriptor 2 65
hibernate jars 4 45
Java Eclipse Loop 3 20
American Express @Work site and Java 4 11
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question