HTML: select

Source: Internet
Author: User

HTML: select tags generate a select element. Whether it is single-choice or multiple-choice depends on the tag's multiple attribute. If multiple = "true" is specified, multiple options are selected. The corresponding attribute should be an array. If multiple = "true" is not specified, it is a single choice. The corresponding attribute should be a scalar.

Note: In order to correctly handle the absence of any selection, the reset () method in actionform must set the scalar attribute to the default value and set the length of the array to 0.

Another important issue is how struts generates the option element. This task struts handed over three tags: HTML: option, HTML: options, and HTML: optionscollection.

HTML: Option tag

HTML: The option tag generates an HTML option element. The tag must be embedded in the HTML: Select tag. Its display text is from its label body or resource file. Its value attribute is used to specify the value to be submitted.

<HTML: option value = "one"> one <HTML: option value = "two"> two HTML: Options tag

The HTML: Options tag generates multiple HTML option elements. The tag must be embedded in the HTML: Select tag. In addition, the working method is somewhat special. Its name and property attributes are different from those of other labels. Let's take a look at its working method.

Specify collection attributes
Collection attribute not specified
Specify collection attributes

Let me use an example to introduce how the label works when specifying the collection attribute. First, we must describe that the persons in selectform are exactly the same as those in listform. See Bean: Define label.

The following code uses the bean: Define tag to get the persons in selectform to the page scope, and then HTML: the options tag then selects this persons based on collection = "personcollection" and generates an option element for each object (person type. The property = "ID" of this tag indicates that the ID attribute of the object (person type) in persons will be used as the value of the Option element. Labelproperty = "name" of the label indicates that the name attribute of the object (person type) in persons is used as the label value of the Option element.

When this select statement is submitted, the selected value is submitted to selectform (name = "selectform ") property = "person. ID ").

<Bean: Define id = "personcollection" name = "selectform"
Property = "persons"/>
<HTML: Select name = "selectform" property = "person. ID" size = "1">
<HTML: Options collection = "personcollection" property = "ID"
Labelproperty = "name"/>
</Html: Select>
Collection attribute not specified

Let me use an example to introduce how the tag works when the collection attribute is not specified. Let's take a look at the definitions of IDs and names:

Private list <string> IDs = NULL;
Private list <string> names = NULL;
The above Code comes from selectform, where IDS is a string list and names is also a string list. We assume that the two lists contain the same number of elements. With these, let's begin to introduce the following code. The HTML: Options tag uses the I value in IDS as the value of the Option element and the value at the same position in names (I value) as the label value of the Option element. If IDS is longer than names, the values in IDs will be the value of option and the label of option. If IDS is less than names, so many names values will be lost.

When this select statement is submitted, the selected value is submitted to selectform (name = "selectform ") property = "person. ID ").

<HTML: Select name = "selectform" property = "person. ID" size = "1">
<HTML: Options property = "IDS" labelproperty = "names"/>
</Html: Select>
HTML: optionscollection tag

HTML: The optionscollection tag generates multiple HTML option elements. The tag must be embedded in the HTML: Select tag. Its functions are the same as those of the HTML: Options tag, but its name and property attributes have the same meaning as those of other tags, which is more natural to understand.

Let me introduce the use of HTML: optionscollection tags through examples. Obtain the persons list in selectform Based on name = "selectform" and property = "persons", and then the objects in the list (person type) as the value (value = "ID") of the Option element, the object in the list (person type) as the label value (Label = "name") of the Option element ").

<HTML: Select name = "selectform" property = "person. ID" size = "1">
<HTML: optionscollection name = "selectform" property = "persons"
Label = "name" value = "ID"/>
</Html: Select>
The following is a multiple-choice example. Although the HTML: Options tag is used in the example, the HTML: option and HTML: optionscollection can also be used for multiple-choice. You must also be aware that the HTML: option, HTML: options, and HTML: optionscollection tags can be used at the same time. In the code, the personids are a string [] type array declared in selectform to receive submitted values.

<HTML: Select name = "selectform" property = "personids"
Multiple = "true" size = "2">
<HTML: Options property = "IDS" labelproperty = "names"/>
</Html: Select>

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/AWUSOFT/archive/2008/05/16/2452874.aspx

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.