Struts2 doubleselect usage and example

Source: Internet
Author: User
Tags key string
1. Example 1:

<S: select list = "{'AA', 'bb ', 'cc'} "theme =" simple "headerkey =" 00 "headervalue =" 00 "> </S: Select>

2. Example 2:

<S: select list = "# {1: 'A', 2: 'bb', 3: 'cc'} "label =" ABC "listkey =" key "listvalue =" value "headerkey =" 0 "headervalue =" AABB ">

List is the set to be displayed, listkey is the ID of the element to be displayed, listvalue is the value of the element to be displayed, headerkey is the ID of the value displayed in the default header, headervalue is the value of the element displayed in the default header.

3. Example 3:

<%

Hashmap map = new linkedhashmap ();

Map. Put (1, "AAA"); map. Put (2, "BBB ");

Map. Put (3, "CCC ");

Request. setattribute ("map", MAP );

Request. setattribute ("AA", "2 ");

%>

<S: select list = "# request. Map" label = "ABC"

Listkey = "key" listvalue = "value"

Value = "# request. AA" headerkey = "0"

Headervalue = "AABB"> </S: Select>

 

<S: select list = "discussions" listkey = "ID" listvalue = "discussionname" name = "seldis" value = "% {subject. discussion. id} "> </S: Select>

This is the select tag in struts. Discussions is a list-type object, and ID is the GETID method in the call object. The value is similar, mainly the value here. It uses the ognl language, the parameter is the ID value of the currently passed object. The select tag determines whether the current entry is displayed by default based on whether the value is the same as the listvalue value. The original selected content can be modified.

 

<S: Select label = "product category" name = "categoryparentid"
Id = "toplist" emptyoption = "true"
Value = "% {commodity. commoditycategory. ID }"
List = "commoditiespagination. Items"
Listkey = "ID" listvalue = "categoryname"
Headerkey = "1" headervalue = "-item type -"
Onchange = "categoryselect ()"/>

 

========================================================== ==================================

Doubleselect is a form tag with slightly complex points in struts2. In particular, the official example is relatively simple and has no practical value.

 

<S: doubleselect label = "doubleselect Test2" name = "menu" list = "# {'fruit': 'Nice fruits ', 'other ': 'oss dishes'} "doublename =" dishes "doublelist =" Top = 'fruit '? {'Apple', 'Orange '}:{ 'monkey ',
'Chicken'} "/>

 

In this official example, we can see the basic usage. The attribute at the beginning of list represents the attribute of the first-level drop-down box, and the attribute at the beginning of doublelist is the second-level drop-down box. it basically corresponds to the properties of the select tag.

 

In practical applications, we pay more attentionList and doublelistList is the data set in the first-level drop-down box, for example, list <fatherbean>, while doublelist can be stored using Map <string, list <sonbean>, the map key string is associated with the level 1 drop-down box.

 

Here is an example of my writing:

First, write two JavaBean, which is generally associated with the database table:

Package com. Student. Bean;

Public classProvince{
Int ID;
String name;
// Set and get Methods

}

Package com. Student. Bean;

Public classCity{
Int ID;
String name;
 Int provinceid;
// Set and get Methods
}

Write the action and send data to and from the web page:

Package com. Student. Action;

Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. List;
Import java. util. Map;
Import com. opensymphony. xwork2.actionsupport;
Import com. Student. Bean. province;
Import com. Student. Bean. City;

Public class doubleselectaction extends actionsupport {
Private list <province> provincelist;
Private Map <integer, list <city> citymap;

Public list <province> getprovincelist (){
Return provincelist;
}

Public void setprovincelist (list <province> provincelist ){
This. provincelist = provincelist;
}

Public Map <integer, list <city> getcitymap (){
Return citymap;
}

Public void setcitymap (Map <integer, list <city> citymap ){
This. citymap = citymap;
}

Public String execute () throws exception {
Provincelist = new arraylist <province> ();

Province province;
Province = new province ();
Province. setid (1 );
Province. setname ("Shanxi Province ");
Provincelist. Add (province );

Province = new province ();
Province. setid (2 );
Province. setname ("Shaanxi Province ");
Provincelist. Add (province );

Province = new province ();
Province. setid (3 );
Province. setname ("Henan Province ");
Provincelist. Add (province );

Citymap = new hashmap <integer, list <city> ();

List <city> citylist = new arraylist <city> ();
City city;
City = New City ();
City. setid (1 );
City. setname ("Taiyuan City ");
City. setprovinceid (1 );
Citylist. Add (city );

City = New City ();
City. setid (2 );
City. setname ("Datong City ");
City. setprovinceid (1 );
Citylist. Add (city );

City = New City ();
City. setid (3 );
City. setname ("Changzhi City ");
City. setprovinceid (1 );
Citylist. Add (city );

Citymap. Put (1, citylist );

Citylist = new arraylist <city> ();
City = New City ();
City. setid (4 );
City. setname ("Xi'an ");
City. setprovinceid (2 );
Citylist. Add (city );

City = New City ();
City. setid (5 );
City. setname ("Hanzhong City ");
City. setprovinceid (2 );
Citylist. Add (city );

City = New City ();
City. setid (6 );
City. setname ("Xianyang City ");
City. setprovinceid (2 );
Citylist. Add (city );

Citymap. Put (2, citylist );

Citylist = new arraylist <city> ();
City = New City ();
City. setid (7 );
City. setname ("Zhengzhou City ");
City. setprovinceid (3 );
Citylist. Add (city );

City = New City ();
City. setid (8 );
City. setname ("Kaifeng City ");
City. setprovinceid (3 );
Citylist. Add (city );

City = New City ();
City. setid (8 );
City. setname ("Luoyang ");
City. setprovinceid (3 );
Citylist. Add (city );

Citymap. Put (3, citylist );

Return success;

}
}

Write the configuration file:

<Action name = "doubleselect" class = "com. Student. Action. doubleselectaction">
<Result name = "success">/tagsdoubleselect. jsp </result>
</Action>

 

Finally, write the web page showing the cascade drop-down menu:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> struts2 cascade drop-down menu example </title>
</Head>
<Body>

<S: form name = "f1">
<S: doubleselect list = "provincelist" listkey = "ID" listvalue = "name"
Doublename = "cityid"
Doublelist = "citymap. Get (top. ID)" doublelistkey = "ID" doublelistvalue = "name"/>
</S: Form>
</Body>
</Html>

 

Note the usage of top. Top indicates the list of currently selected objects, so top. ID corresponds to the ID in the currently selected province object, citymap. get (top. ID) is used to retrieve the data set in the second-level drop-down Box Based on the ID of the currently selected province object.

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.