Using the Struts HTML tag library.
R. Kevin ColeApril 23, 2003

The HTML tag library included with the Struts distribution, is a set of page construction tools that correspond closely with standard HTML elements. One of these tags, the select tag, has been a source of confusion for many developers, me included. This article is an introduction to the html:select and the html:optionsCollection tags available in the jakarta Struts HTML tag library.

The Select Tag

The Select tag gives the user the ability to choose from a fixed set of values; one or several at once. A web browser will render the select tag as select/options list in your web page. For instance:

<select>
<option>apples</option>
<option SELECTED>oranges</option>
<option>pearsi</option>
</select>

This HTML paragraph creates the combobox and sets the default selection to oranges.

The Select Tag in the HTML Tag Library

The Struts html tag library offers the select tag with two versions of the options tags.

<html:select name="defaultValue" property="value">
<html:optionsCollection name="myKey" value="value" label="label"/>
</html:select>
Listing 2: A basic select/options configuration in the HTML tag library.

In this example, defaultValue is the key for a bean in the current session context that has a property called value and a corresponding getter called getValue(). The default option in the select/options group will be set using the String returned by defaultValueBean.getValue().

In the html:optionsCollection tag, myKey is the key for a bean in the current session context. The bean that myKey references must be to a class that extends one of the java Collection classes. The myKey bean is a Collection of objects. The value and label parameters indicate that each object in the Collection will have getter methods called getValue() and getLabel().

For each object in the myKey collection there will be a corresponding "option" tag created with a value set by object.getValue() and a label set by object.getLabel(). The default option is determined by defaultValue.getValue().

A Simple Example

As an example, let's say we need to create a form that includes a U.S. state selection combobox. A struts action class SelectStateAction is presented in example 4 along with its corresponding action form, to receive the results of our state selection.

<html:form action="SelectStateAction.action" method="post">
<html:select name="currentState" property="id">
<html:optionsCollection name="states" value="id" label="label"/>
</html:select>
</html:form>
Listing 1: A struts JSP page with an "html:select/options" block
 

The states string is a key that references an instance of our StatesList class in the current session context. That is, we could access this object in a JSP scriptlet or in a servlet using the following code:

StatesList stateList = (StateList) session.getAttribute("states");

The StatesList is an extension of an ArrayList. This satisfies the requirementthat it be a java Collection.

public class StatesList extends ArrayList implements Serializable
{
public StatesList()
{
super(50);

add( 0, new OptionItem("AL", "Alabama" ) );
add( 1, new OptionItem("AK", "Alaska") );
.
.
.
add( 49, new OptionItem("WY", "Wyoming") );
}
}
Listing 2: Creating a collection of objects to populate the select/options block.

So the StatesList is a collection; but a collection of what? Recall that in the declaration of our htmlOptions tag

 <html:optionsCollection name="states" value="id" label="label"/>

we have stated that our StatesList (referenced in the session context as states) will be a collection of objects that have properties named id and label and that these two properties will become the value and label of the options in an HTML select tag.

We can implement these objects in the StatesList as an instance of class OptionItem. OptionItem is a class that that we create simply to hold the value and label parameters of an option tag. OptionItem has the requisite properties (read-only in this case) named id and label and the getter methods getId() and getLabel().

public class OptionItem
{
private String id;
private String label;

public String getId() { return this.id; }
public String getLabel() { return this.label; }

public OptionItem( String id, String label )
{
this.id = id;
this.label = label;
}
}
Listing 3: The OptionItem class has properties and methods that map the components of an HTML option element.

The default selection as stated in the select tag is also an instance of the OptionItem class. This is not a requirement, it is just convenient. It could be any bean with a getId() method.

In a Struts action class we could create the states collection in the following manner. First checking to see whether the StatesList object has already been added to the session and then creating it if it hasn't. The default value for the combobox is also defined and placed in session under the currentState key.

public final class SelectStateAction extends Action 
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException
{ .
.
.

StatesList states = (StatesList) session.getAttribute("states");
if( states == null )
{
states = new StatesList();
session.setAttribute("states", states );
session.setAttribute("currentState", states.get(0) );
} .
.
.
}
}
Listing 4: A portion of an action class that checks the current session for the presence of a StatesList and if it isn't present, creates one and places it in the current session.

 

Copyright © 2003 R. Kevin Cole