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.
1.<%@taglib
uri="/WEB-INF/struts-html.tld" prefix="html" %>
To use the Struts
HTML Tag Library you need to add the following <taglib> subelement to the
web.xml file.
1.<taglib>
2.<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
3.<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
4.</taglib>
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.
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="/inputAction" >
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>
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.
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>
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.
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;
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.
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"));
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.
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"));
The following code
is used to dispaly the state list in the jsp page.
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>
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.
No comments:
Post a Comment