MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to populate a drop down box with Select Tag in JSP page

We will learn today how to populate a drop down box in the jsp page with the help of a select tag

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

The purpose of this tutorial is to show readers two different methods which are required to populate a dropdown box in the jsp page. This is done with the help of a select tag in order to do this. As far as the dropdown values are concerned, they are stored in two ArrayList. The Array lists are namely countryList and stateList. Making use of the HTML optionsCollection tag, I is done so as to display the values that are present in the ArrayList.

The below listed taglib directive in the jsp page needs to be included so as to use the Struts HTML Tags.

Listing 1: Code displaying the taglib directive

<%@taglib uri=”/WEB-INF/struts-html.tld” prefix="html" %>

The following <taglib> subelement needs to be incorporated to the web.xml file so as to use the Struts HTML Tag Library.

Listing 2: Code displaying the <taglib> subelement

<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

Talking about the jsp page, it comprises of the two dropdowns. One is used for displaying the country and the other one is used for displaying the state. The following code is contained by the jsp page.

Listing 3: Code contained by the jsp page

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<html:form action="/inputAction" >
<table>
<tr>
<td>
Select Country :
</td>
<td>
<html:select property="country" >
<html:option value="0">Select Country</html:option>
<html:optionsCollection name="InputForm" property="countryList"label="countryName" value="countryId" />
</html:select>
</td>
</tr>
<tr>
<td>
Select State :
</td>
<td>
<html:select property="state" >
<html:option value="0">Select State</html:option>
<html:optionsCollection name="InputForm" property="stateList"label="label" value="value" />
</html:select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<html:submit property="method" value="save" />
</td>
</tr>
</table>
</html:form>
</body>
</html>

The CountryData class is made use of in order to hold the details regarding the country. The CountryData class has a countryId and countryName as attributes and the corresponding getter and setter methods. We make use of the following code in the jsp page in order to display the country list.

Listing 4: Code displaying in the jsp page in order 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>

The objective here is to display the countryName as the label and then the countryId as the corresponding value which is done making use of the label and the value attribute of the HTML optionsCollection tag.

Talking about the property attribute of the HTML optionsCollection tag, it holds the ArrayList.

As far as the property attribute of the HTML select tag is concerned, it holds the country value that is being selected by the user.

We have the following attributes present in the input form:

Listing 5: Attributes available in the input form

// holds the country selected by the user
private String country;
// holds the state selected by the user.
private String state;
// holds the list of countries to be displayed.
private ArrayList countryList;
// holds the list of state to be displayed.
private ArrayList stateList;

Here we are making use of the DispatchAction. We have one method that is utilized so as to populate the values. And then we have the second method which is used to save the selected values. We populate the values for countryList and thestateList inside the populate method. The below or the following code is made use of in order to add a list of countries to the countryList.

Listing 6: Code displaying the process to add a list of countries to the countryList

ArrayList countryList = new ArrayList();
countryList.add(new CountryData("1", "USA"));
countryList.add(new CountryData("2", "Canada"));
countryList.add(new CountryData("3", "Mexico"));

The CountryData object is added inside the countryList and creating a seperate class like this and then incorporating or adding it to ArrayList will be very significant in case we have a few more attributes and methods corresponding to it other than the label and value.

In case we are just equipped with label and a value, in that case we can make use of the LabelValueBean class. This is used so as to incorporate the label and value to the ArrayList. We populate the stateList making use of the LabelValueBean class. Below displays the code so as to populate the stateList in the action class:

Listing 7: Code displaying 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"));

Below is the code that is made use of to display the state list in the jsp page.

Listing 8: Code displaying the process to display the state list in the jsp page

<html:select property="state" >
<html:option value="0">Select State</html:option>
<html:optionsCollection name="InputForm" property="stateList"
label="label" value="value" />
</html:select>

The below screen displays the output to the user on running the above implementation code:

output to the user on running the above implementation code

Figure 1: output to the user on running the above implementation code

When you drill down the country and the state dropdowns, they are populated with the values that are present in the array list.

populated with the values that are present in the array list

Figure 2: populated with the values that are present in the array list

Conclusion

The tutorial intended to make the users learn on the process how to populate a drop down box with the help of a Select tag.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login