Struts html: the checkbox box is selected by default.

Source: Internet
Author: User

How to make the checkbox box selected by default when using the struts tag html: checkbox, generally, when the value of the Property in Formbean is equal to the value specified on the tag, the generated jsp page is selected.

In Struts, labels can be selected at the beginning. In Formbean, the Property values set to 1.on, 2.yes, and 3. true can be selected by default.

Note: If it is not selected, it is best to set the Property to "" because the Property value is null in the Action. If you do not pay attention to it, an exception will occur.

Copy codeThe Code is as follows: public class CheckBoxForm extends ActionForm {
// Private String id = "";
Private String id = "on ";
Public String getId (){
Return id;
}
Public void setId (String id ){
This. id = id;
}
}

Jsp pageCopy codeThe Code is as follows:

Like: <input type = "checkbox" name = "id" checked = "checked"> CheckBox </input> or <input type = "checkbox" name = "id"> CheckBox </ input>
The specific business is processed in the Action ,"". equals (actionForm. getId () or "on ". equals (actionForm. getId () determines whether a property is selected, and determines whether a property is selected. If no property is set in ActionForm, null is used for determination.

1) Requirements:
When you enter the page through the menu, the checkbox on the page is selected, after the page is submitted, forward returns to this page, the corresponding checkbox remains in the state of submission, that is: if the status of the checkbox is selected, the status of the checkbox is returned, and the status of the checkbox is selected before submission, the checkbox should be selected.

2) Problem:
This requirement looks simple, just
1) set the attribute of checkbox to true in the corresponding form:
Private boolean syaken = true;
2) use the html: checkbox tag in the corresponding jsp.
<Html: checkbox property = "syaken"/>
But the problem is that the checkbox tag does not go to the checkbox attribute whose set initialization value is true. If no initial value is assigned, Struts is assigned false by default;
Someone says that the reset () method that implements ActionFrom sets the corresponding property value to false, but the reset method is called after the form is instantiated, that is to say, the value you initialize will be reset to false, which is no different from setting the property value to false directly, the result is that the checkbox is not selected when you enter the page from the menu.
There is a conflict between them. The key to the problem is that at each commit, Struts does not reset the attribute values of all checkboxes, but is selectively reset (the initial value is false.

3) Solution:
It is super simple. Add a hidden input box with the same name as the checkbox attribute and the value is "false" after the checkbox, and force Struts to reset the checkbox attribute value:
<Html: checkbox property = "syaken"/>
<Input type = "hidden" name = "syaken" value = "false">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.