Struts HTML: the checkbox tag is too depressing

Source: Internet
Author: User

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"/> & nbsp;
<Input type = "hidden" name = "syaken" value = "false">
========================================================== ========================================================== =
The above is reprinted.

After my attempt, I think this method works.

In fact, multibox also has similar problems.
Suppose: In form, private string [] seqnum = NULL;
When seqnum is 1, the number of multiboxes displayed on JSP is one.
At this time, use the following in javascript:
Document. All. seqnum [0]. Through Alert (document. All. seqnum [0]), the result is undefined, which is not the expected checkbox object.
If the length of seqnum is greater than 1, the preceding problem does not occur.

At the same time, if the seqnum length is 1, when the multibox is submitted to the background, the selected status of the above text and the checkbox is incorrect.

The solution is to add a hidden domain.
<Input type = "hidden" name = "seqnum" value = "-1">
In this way, the length of seqnum is increased to 1, and the submission problem is solved temporarily.

Assume that a view has a multibox image and corresponds to three groups of data.
Perform the following operations: 1. Hook the checkbox of the first record, submit, and form to obtain the first data record.
2. Hook the checkbox on the hook and submit it. form still gets the first data,
We do not think seqnum = NULL or seqnum. Length = 0.

The solution is still the original method:
<Input type = "hidden" name = "seqnum" value = "false">
Value can be used theoretically as long as it is not confused with other multiboxes.

Note:
Document. All. seqnum [0] indicates hiding the seqnum value.

Document. All. seqnum [1] indicates the multibox on the real screen.

However, doing so will also cause troubles for some other operations and algorithms, which is not omnipotent.

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.