File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes How to check a checkbox automatically in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to check a checkbox automatically in struts" Watch "How to check a checkbox automatically in struts" New topic

How to check a checkbox automatically in struts

dimpsonu arora
Ranch Hand

Joined: Aug 13, 2003
Posts: 51
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

Joined: Feb 22, 2005
Posts: 261
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

Joined: Aug 13, 2003
Posts: 51
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.
Jatinder Arora.
[ January 20, 2006: Message edited by: dimpsonu arora ]
jakeer ahmed

Joined: Jan 17, 2006
Posts: 15
Hellow friend u can set the check box state in action class by using
similarly u can uncheck by form.setProduct("off");
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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


Or, if the value comes from a database, populate the product property with a call to the database.

Consultant, Sima Solutions
Ganesh Anbhule

Joined: Jun 30, 2008
Posts: 1
You can use the constructor of your java bean.


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

Joined: Jan 08, 2009
Posts: 1
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;
I agree. Here's the link:
subject: How to check a checkbox automatically in struts
jQuery in Action, 3rd edition