Struts SelectTagSample
来源:互联网 发布:js 扫描 编辑:程序博客网 时间:2024/06/04 18:02
In this example we will see two different methods to populate a dropdown box in the jsp page. We use HTML select tag to do this. The dropdown values are stored in two ArrayList namely countryList and stateList. We use HTML optionsCollection tag to display the values present in the ArrayList. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page. To use the Struts HTML Tag Library you need to add the following <taglib> subelement to the web.xml file. The jsp page contains two dropdowns one for displaying the country and the other for displaying the state. The jsp page contains the following code. The CountryData class is used to hold the details regarding the country. The CountryData class has countryId and countryName as attributes and the corresponding getter and setter methods. In the jsp page we use the following code to display the country list. Here we need to display the countryName as the label and the countryId as the corresponding value. We do this using the label and the value attribute of the HTML optionsCollection tag. The property attribute of the HTML optionsCollection tag holds the ArrayList. The property attribute of the HTML select tag hold the country value selected by the user. The input form contains the following attributes. Here we use DispatchAction. One method is used to populate the values and the other method is used to save the selected values. Inside the populate method we populate the values for countryList and thestateList. The following code is used to add a list of countries to the countryList. We add CountryData object inside the countryList. Creating a seperate class like this and adding it to ArrayList will be helpful if we have a few more attributes and methods corresponding to it other than the label and value. If we have only label and value then we can use the LabelValueBean class to add the label and value to the ArrayList. We populate the stateList using the LabelValueBean class. The following code is used to populate the stateList in the action class. The following code is used to dispaly the state list in the jsp page. On executing the example the following page will be dispalyed to the user. The country and the state dropdowns are populate with the values present in the array list. You can download the source code of the HTML Select Tag example by clicking on the Download link below. 点击这里下载例子代码Struts HTML Tag
1.
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
1.
<
taglib
>
2.
<
taglib-uri
>/WEB-INF/struts-html.tld</
taglib-uri
>
3.
<
taglib-location
>/WEB-INF/struts-html.tld</
taglib-location
>
4.
</
taglib
>
01.
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
02.
<
html
>
03.
<
head
>
04.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
05.
<
title
>JSP Page</
title
>
06.
</
head
>
07.
<
body
>
08.
<
html:form
action
=
"https://youtubeproxy.org/default.aspx"
>
09.
<
table
>
10.
<
tr
>
11.
<
td
>
12.
Select Country :
13.
</
td
>
14.
<
td
>
15.
<
html:select
property
=
"country"
>
16.
<
html:option
value
=
"0"
>Select Country</
html:option
>
17.
<
html:optionsCollection
name
=
"InputForm"
property
=
"countryList"
label
=
"countryName"
value
=
"countryId"
/>
18.
</
html:select
>
19.
</
td
>
20.
</
tr
>
21.
<
tr
>
22.
<
td
>
23.
Select State :
24.
</
td
>
25.
<
td
>
26.
<
html:select
property
=
"state"
>
27.
<
html:option
value
=
"0"
>Select State</
html:option
>
28.
<
html:optionsCollection
name
=
"InputForm"
property
=
"stateList"
label
=
"label"
value
=
"value"
/>
29.
</
html:select
>
30.
</
td
>
31.
</
tr
>
32.
<
tr
>
33.
<
td
colspan
=
"2"
align
=
"center"
>
34.
<
html:submit
property
=
"method"
value
=
"save"
/>
35.
</
td
>
36.
</
tr
>
37.
</
table
>
38.
</
html:form
>
39.
</
body
>
40.
</
html
>
1.
<
html:select
property
=
"country"
>
2.
<
html:option
value
=
"0"
>Select Country</
html:option
>
3.
<
html:optionsCollection
name
=
"InputForm"
property
=
"countryList"
4.
label
=
"countryName"
value
=
"countryId"
/>
5.
</
html:select
>
1.
// holds the country selected by the user
2.
private
String country;
3.
// holds the state selected by the user.
4.
private
String state;
5.
// holds the list of countries to be displayed.
6.
private
ArrayList countryList;
7.
// holds the list of state to be displayed.
8.
private
ArrayList stateList;
1.
ArrayList countryList =
new
ArrayList();
2.
countryList.add(
new
CountryData(
"1"
,
"USA"
));
3.
countryList.add(
new
CountryData(
"2"
,
"Canada"
));
4.
countryList.add(
new
CountryData(
"3"
,
"Mexico"
));
1.
ArrayList stateList =
new
ArrayList();
2.
stateList.add(
new
LabelValueBean(
"New York"
,
"1"
));
3.
stateList.add(
new
LabelValueBean(
"California"
,
"2"
));
4.
stateList.add(
new
LabelValueBean(
"Los Angeles"
,
"3"
));
1.
<
html:select
property
=
"state"
>
2.
<
html:option
value
=
"0"
>Select State</
html:option
>
3.
<
html:optionsCollection
name
=
"InputForm"
property
=
"stateList"
4.
label
=
"label"
value
=
"value"
/>
5.
</
html:select
>
- Struts SelectTagSample
- Struts
- STRUTS
- struts
- Struts
- Struts
- Struts
- Struts
- Struts
- Struts
- Struts
- Struts
- struts
- struts
- struts
- Struts
- struts
- Struts
- 绕任意轴旋转的矩阵推导
- windows2003+iis6+php+mysql 服务器配置
- 部分排序算法的实现
- 在这里安家了
- 如何从 itemRenderer 中分派事件[Flex][翻译]
- Struts SelectTagSample
- 使用Word常见问题的解决
- iPhone开发进阶(5) — 编程定制UIViewController
- php开发50个非常有用的工具【转】
- 是时候了!
- 不亮的点阵
- iPhone开发进阶(6)— 编程定制UIButton
- iPhone开发进阶(7)— 利用ModalViewController切换View
- SharePoint SDK 中文版