FAQ: Why are my checkboxes not being set from ON to OFF?

By: Apache Foundation Emailed: 1730 times Printed: 2344 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

A problem with a checkbox is that the browser will only include it in the request when it is checked. If it is not checked, the HTML specification suggests that it not be sent (i.e. omitted from the request). If the value of the checkbox is being persisted, either in a session bean or in the model, a checked box can never unchecked by a HTML form -- because the form can never send a signal to uncheck the box. The application must somehow ascertain that since the element was not sent that the corresponding value is unchecked.

The recommended approach for Struts applications is to use the reset method in the ActionForm to set all properties represented by checkboxes to null or false. The checked boxes submitted by the form will then set those properties to true. The omitted properties will remain false. Another solution is to use radio buttons instead, which always submit a value.

It is important to note that the HTML specification recommends this same behavior whenever a control is not "successful". Any blank element in a HTML form is not guaranteed to submitted. It is therefor very important to set the default values for an ActionForm correctly, and to implement the reset method when the ActionForm might kept in session scope.


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

You can also include a hidden field of the same name preceding the checkbox. Assign this field the opposite value as the checkbox. If the checkbox is checked upon submission, the checkbox value is sent, otherwise the preceeding value is sent.

<INPUT TYPE=HIDDEN NAME=ENABLED VALUE=0>
<INPUT TYPE=CHECKBOX NAME=ENABLED VALUE=1> // We display enabled twice in order to pass the correct value even when not checked


View Tutorial          By: Stray at 2010-01-05 08:07:47

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Apache Foundation
When is the best time to validate input in Struts
Chaining actions in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
What is Struts? Which Version of Struts to use?
Struts 1 vs Struts 2
Using JavaScript to submit a form in Struts
Configuring JDBC DataSources in Struts
MVC Architecture (Model 2 Architecture)
How to prepopulate a form in Struts
Installing and configuring Tomcat and Struts
Handling Duplicate Form Submissions in Struts
Model 1 Architecture
ForwardAction in Struts
7 Best Practices of Struts
What is a Plug-in and how to use Java plug-ins with Struts?
FAQ: Why are my checkboxes not being set from ON to OFF?
History of Struts
Most Emailed Articles (in Struts)
Downloading and installing Struts
The directories and files of a Struts application
What is Struts? Which Version of Struts to use?
WebWork 2 is now Apache Struts 2
ForwardAction in Struts
Origin and Architecture of Struts
Simple example of using the requiredif Validator rule in Struts
Is Struts the most popular web application framework for Java?
What is a Model View Controller (MVC) Model?
History of Struts
Configuring JDBC DataSources in Struts
Struts Classes
Editing web.xml in a Struts Application
Editing struts-config.xml in a Struts Application
Creating the first application using Struts 2