Struts html:checkbox Box Initial default is the selected workaround _jsp programming

Source: Internet
Author: User

When using the Struts tab Html:checkbox, how to make the checkbox default is selected, usually when the value of the property in Formbean and the value given on the label is equal, the generated JSP page is selected.

Struts in several situations can make the label initial is selected, Formbean inside the value of the property set to 1.on, 2.yes, 3.true of these three kinds of situations can be implemented by default selected.

Note : If you do not select, it is best to set the property to "", because in the action to get the value is null, in the case of less attention, it will appear an exception.

Copy Code code 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 page
Copy Code code as follows:


As follows: <input type= "checkbox" Name= "id" checked= "checked" >CheckBox</input> or <input "checkbox" type= "id" >CheckBox</input>
The specific business is handled in action, "". Equals (Actionform.getid ()) or "on". Equals (Actionform.getid ()) One is judged not checked, one is judged selected, If the property is not set in the Actionform, it should be judged by NULL.

1) Demand
When you enter the page through the menu, the checkbox in the page is selected, the page is submitted forward back to this page, the corresponding checkbox to maintain the status of the submission, that is to say: If the checkbox is selected when the submission is still selected state If the checkbox is removed by the user before submitting it, the checkbox should be selected when it returns.

2) Problem
This requirement looks simple and requires only
1 set the checkbox's corresponding property to true in the corresponding form:
Private Boolean Syaken = true;
2 the corresponding JSP in the use of Html:checkbox tags on the line
The problem is that the checkbox label does not go to the CheckBox property with the set initialization value of true, and struts defaults to false when the initial values are not assigned;
Looking for data from the Internet someone said you can implement Actionfrom. The Reset () method sets the corresponding property value to False, but the reset method is invoked after the form instantiation, meaning that the value you initialize is reset to False. This is no different from setting the property value to false directly, as a result of which the checkbox is not selected when you enter the page from the menu.
There is a contradiction between them, the crux of the problem is that each time the submission, struts did not reset all the CheckBox property values, but selectively (initialization value is false) to reset.

3) Solutions
Super simple, add a hidden input box with the checkbox after the checkbox with the same name and value "false" and force struts to reset the property values for the checkbox:
<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.