Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting 'unchecked' value from Struts checkbox

Posted on 2004-08-23
10
Medium Priority
?
1,347 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

886 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