aspose file tools*
The moose likes Struts and the fly likes struts-html:checkbox  default checked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts-html:checkbox  default checked" Watch "struts-html:checkbox  default checked" New topic
Author

struts-html:checkbox default checked

Ratan Kumar
Ranch Hand

Joined: Jul 19, 2006
Posts: 67
Hi,

I want to use struts-html:checkbox to insert/update a field in a table.

I am using dynavalidatoractionform in struts-config and beanutils.copyproperties to populate these fields in a formbean

i need this checkbox to be checked by default when the page comes up. on submit, it should send Y if checked / N if unchecked to the actionclass.

i tried this, by using attribute "initial" in dynavalidatoractionform in struts-config . so by default it gets checked. but when i uncheck it, it still goes as checked to the actionclass

do i have to use hidden/reset method to solve this problem ?
Pls help with some code fragments
[ October 27, 2007: Message edited by: Ratan Kumar ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This problem has to do with the fact that in HTML, no value is submitted for an unchecked checkbox when a form is submitted. The solution to the problem in your case would be to set the initial value to "N" in the struts-config.xml file. Then add code to set it to "Y" before the page is initially displayed. You could put this code in an action that forwards to the JSP.


Merrill
Consultant, Sima Solutions
Ratan Kumar
Ranch Hand

Joined: Jul 19, 2006
Posts: 67
Thanks for the reply

"Then add code to set it to "Y" before the page is initially displayed. You could put this code in an action that forwards to the JSP."

Can i put this code in the jsp(using onload) or in the formbean(in setter) after beanutils.copyproperties is executed instead of the action class?if yes, how?

does using hidden field in jsp or reset() method in the formbean can also solve this prob?

Thanks again
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You could update it in the jsp with code similar to the following just before your <html:checkbox> tag:

The problem with doing it this way is validation. If the page is redisplayed due to validation errors, this code always gets executed, so it will always cause the checkbox to be checked regardless of whether the user unchecked it or not. To get around that, you could do something like this:

[ October 28, 2007: Message edited by: Merrill Higginson ]
Ratan Kumar
Ranch Hand

Joined: Jul 19, 2006
Posts: 67
Thanks for that.I need to try that out.

I have more question.

here in i am using struts dynaactionform and from there i copy the properties in a javabean. Can i implement reset() method in thhis javabean(this bean doesnot extend actionform). this question is just for my information.

Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Ratan Kumar:
Can i implement reset() method in thhis javabean(this bean doesnot extend actionform).

You can write a reset method in your bean, but Struts will never call it since the bean isn't declared as the ActionForm bean, and doesn't extend the ActionForm class.
Ratan Kumar
Ranch Hand

Joined: Jul 19, 2006
Posts: 67
It worked.

Had to use html:hidden for that

c:set gave some error and i could not reset as i am using dynaactionform and dont want to subclass it.

Thanks for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts-html:checkbox default checked