Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check a checkbox automatically in struts

 
dimpsonu arora
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am facing a problem in struts. My requirement is to check a checkbox automatically when the form is loaded. In HTML, We can do the same by following code.
<input type="checkbox" name="product" value="Rice">
<input type="checkbox" name="product" value="Wheat" checked>
<input type="checkbox" name="product" value="Sugar">
The above html code will show three checkboxes with second check boxes as checked. Same thing i want to perform using struts. But unable to do it.
<html:checkbox property="product" value="Rice" />
<html:checkbox property="product" value="Wheat" checked="true" />//Error as no property like checked.
<html:checkbox property="product" value="Sugar" />

Please help as it is very urgent.

Thanks in advance.
Jatinder Arora.
 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are facing this problem because when the JSP is rendered, the compiler checks for the method getChecked() in the corresponding form bean class. However, I don't think you are using any form bean class to represent your JSP page.

Anyways, you can try using the "onLoad" attribute of JavaScript within the <BODY> tag of your page. Then get the check box checked using the JavaScript function you call onLoad.
[ January 20, 2006: Message edited by: Anirvan Majumdar ]
 
dimpsonu arora
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear, I am using FormBean corresponding to my JSP page. I don't want to use java script. Can you please explain the functionality of getChecked() method.
Thanks,
Jatinder Arora.
[ January 20, 2006: Message edited by: dimpsonu arora ]
 
jakeer ahmed
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hellow friend u can set the check box state in action class by using
form.setProduct("on");
similarly u can uncheck by form.setProduct("off");
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The <html:checkbox> tag will automatically check the box if the value of product property of your form bean is "Wheat". To make sure the checkbox for "Wheat" is checked, in the action that forwards to this JSP, just include the statement

myForm.setProduct("Wheat");

Or, if the value comes from a database, populate the product property with a call to the database.
 
Ganesh Anbhule
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the constructor of your java bean.

Like:--


private String checkedOption;

public myJavaBean()
{
setCheckedOption("value"); // This value should be with the value on jsp's value tag.
}

By doing this, it will be automaticaly set the checkBox checked for that checkBox which having specified value.
 
Vipin Huddar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
document.FormName.checkBoxPropertyName.checked = true;

Write this statement in window.onload function of Javascript as shown below:

window.onload = function(){
document.FormName.checkBoxPropertyName.checked = true;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic