Struts html: the checkbox box initially defaults to the selected solution

  • 2020-05-17 06:08:49
  • OfStack

When using the struts tag html:checkbox, how to make the checkbox box be selected by default? In general, when the value of Property in Formbean is the same as the value of value on the tag, the jsp page is selected.

In Struts, the tag can be selected initially in several cases. In Formbean, the value of Property is set to 1.on, 2.yes, and 3.true, all of which can be selected by default.

Note: if it is not selected, it is better to set Property to "", because in Action, Property is null value. If you do not pay much attention to it, it will be abnormal.

 
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 page
 
<html:checkbox property="id">CheckBox</html:checkbox> 

Like: < input type="checkbox" name="id" checked="checked" > CheckBox < /input > or < input type="checkbox" name="id" > CheckBox < /input >
"".equals (actionForm.getId ()) or "on".equals(actionForm.getId ()). If property is not set in ActionForm, null is used to make the judgment.

1) requirements:
When entering the page through the menu, checkbox in the page is in the selected state. After the page is submitted, forward returns to the page and the corresponding checkbox remains in the state when submitting. In other words, if checkbox is in the selected state when submitting, it will still be in the selected state. If the user checks checkbox before submitting, checkbox should return as checked.

2) questions:
This requirement looks simple, just needs
1) set the corresponding property 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"/ >
The problem is that the checkbox tag does not go to the checkbox attribute whose initial value of set is true. Struts is assigned false by default when the initial value is not assigned.
From the Internet to find someone that can realize reset ActionFrom () method will be set to the corresponding attribute value false, but reset method is invoked after form instantiation, which means you true initialized values will be reset to false, this and the attribute value is set to directly false no difference, the result is checkbox when you enter the page from the menu is in the condition of not selected.
There is a contradiction between them. The key problem is that Struts does not reset all the property values of checkbox every time it is submitted. Instead, it is reset selectively (with the initial value of false).

3) solutions:
Super simple, after checkbox add a hidden input box with the same name as checkbox's property and value as "false", force Struts to reset the property value of checkbox:
< html:checkbox property="syaken"/ >
< input type="hidden" name="syaken" value="false" >


Related articles: