When using the struts tag html:checkbox, how to make the checkbox box checkbox by default. Generally, when the value of the Property in Formbean is equal to the value given on the value on the tag, the generated jsp page is selected.
In Struts, the label can be selected at the beginning. The value of the Property in Formbean can be set to , , and the default selection can be achieved.
Note: If not selected, it is best to set the Property to "", because the Property in Action is null value, and if you don't pay much attention, it will cause an exception.
Copy the 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) {
= id;
}
}
jsp page
Copy the codeThe code is as follows:
<html:checkbox property="id">CheckBox</html:checkbox>
As in: <input type="checkbox" name="id" checked="checked">CheckBox</input> or <input type="checkbox" name="id">CheckBox</input>
The specific business is handled in Action, "".equals(()) or "on".equals(()) One is to judge that it is not selected, and the other is to judge that it is selected. If no property is set in ActionForm, then null must be used to make the judgment.
1) Requirements:
When entering the page through the menu, the checkbox in the page is in the selected state. After the page is submitted, forward back to this page, and the corresponding checkbox remains at the time of submission. That is to say, if the checkbox is selected when submitting, it is still selected when it comes back; if the user removes the checkbox checkbox before submitting, the checkbox should be selected when it comes back.
2) Problem:
This requirement seems simple, just
1) Just set the corresponding property of checkbox to true in the corresponding form:
private boolean syaken = true;
2) Just use the html:checkbox tag in the corresponding jsp
<html:checkbox property="syaken"/>
But the problem is that the checkbox tag will not set the checkbox attribute with the initial value of true, and Struts is assigned to false by default when the initial value is not assigned;
Looking for information from the Internet Some people say that ActionFrom's reset() method can be implemented to set the corresponding property value to false, but the reset method is called after form instantiation, that is, the value you initialized is true will be reset to false, which is no difference from directly setting the property value to false. The result of this is that checkbox is in an unselected state when you enter the page from the menu.
There is a contradiction between them. The key to the problem is that every time you submit, Struts does not reset all checkbox attribute values, but selectively (the initialization value is false).
3) Solution:
It's super simple. Add a hidden input box behind the checkbox with the same name as the checkbox attribute and the value is "false", and force Struts to reset the checkbox attribute value:
<html:checkbox property="syaken"/>
<input type="hidden" name="syaken" value="false">