Summarize the usage of the drop-down box on the Web
Obtain the data list from the database and put the data in the request.
Use setattribute ("alist", alist)
A has two attributes (string ID, string value)
1. Use the jstl foreach Method
<Select name = "XX" ......>
<C: foreach items = "$ {alist}" Var = "p">
<C: Choose>
<C: When test = "$ {xxx = P. ID}">
<Option value = '<C: Out value = "$ {P. ID}"/>' selected = "selected">
<C: Out value = "$ {P. Value}"/>
</Option>
</C: When>
<C: otherwise>
<Option value = '<C: Out value = "$ {P. ID}"/>'>
<C: Out value = "$ {P. Value}"/>
</Option>
</C: otherwise>
</C: Choose>
<C: foreach>
</SELECT>
2. Use struts labels
<HTML: Select Property = "XXX">
<HTML: Options collection = "alist" labelproperty = "value" property = "ID"/>
</Html: Select>
Check the struts API documentation. You can see that three taglib options in select can be used.
The first method is to directly write all options in the middle.
<HTML: option value = "0-15"> 0-15
Type 2: Place options in a collection (list is used here). In actual projects, data may come from DB, files, and so on. This situation is often used.
<HTML: Options collection = "alist" property = "value" labelproperty = "label"/> process the option in the action when it is placed in the list. // prepare the age selector list. list agelist = new arraylist (); agelist. add (New labelvaluebean ("0-15", "0-15"); agelist. add (New labelvaluebean ("15-20", "15-20"); agelist. add (New labelvaluebean ("20-30", "20-30"); agelist. add (New labelvaluebean ("30 or abve", "30 or abve"); Request. setattribute ("alist", alist );
Labelvaluebean is used.
<HTML: Options collection = "alist" labelproperty = "value" property = "ID"/>
As long as the bean entered in alist has the value and ID attributes
Third, use this list as an attribute of form.
<HTML: optionscollection property = "alist"/>
Add setter and getter. Form of alist to form as follows.
// The list can be a form property.
F. setagelist (alist );
1. obtain data from the database. You should retrieve data from the action and put the data in the request.
2. Put the data in a list, collection, or map.
3. Use <HTML: Options> or <HTML: optionscollection> to retrieve data from the list or other containers.
4. The <HTML: Options> and <HTML: optionscollection> outer layers must use <HTML: Select Property = "">. Therefore, this attribute must be defined in formbean.
5. Because you need to use these labels, you must define formbean
6.
Retrieve data from action. The following uses list as an example.
List list = XXXXX; // retrieve data from the database drop-down list
Request. setattribute ("list", list );
Displayed on the page
<HTML: Form Action = "XXXX">... <HTML: Select Property = "XXX"> <HTML: Options collection = "list" labelproperty = "content displayed in the drop-down list, it is generally the value corresponding to the name or other similar properties "property =", usually ID "/>
Add a little bit:
Because data needs to be retrieved from the database, Dao is generally called in the action to be uploaded to the page as an attribute of the request. In this case, the <HTML: Options.../> label is generally used.
In addition, if the data is not retrieved from the database, but the code is fixed, it is generally put in actionform as an attribute to be retrieved on the page. In this case, <HTML: optionscollection... />