Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts-html:checkbox default checked

 
Ratan Kumar
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ratan Kumar
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic