Search WebSpherePower's 6,962 WebSphere, Java, and Eclipse article archive 
Home
EasyPrint
News details Click here for the RSS feed's XML code. This is not a browser URL.
Articles-only Click here for the RSS feed's XML code. This is not a browser URL.
Twitter Feed Click here for the Twitter feed.
PROGRAMMING POWER
Using a reusable code approach to HTML select option lists: part II
By Jeff Chilton

"It is best to do things systematically, since we are only human, and disorder is our worst enemy."
-- Hesiod (ancient Greek poet, circa 850 B.C.)

About this series
I've got some serious mea culpa action goin' in this article. As you no doubt noticed, this article is Part II. But where, you might ask, is Part I? Well, here's the story...when it was submitted for editing, I didn't know which ZATZ magazine it was intended for. He's written for DominoPower, so I just assumed that our friend Part I was also intended for DominoPower. Uh, it wasn't. It's all about WebSphere. Fortunately, both Senior Technical Editor Dan Velasco and Contributing Editor Jeff Chilton have a far better grasp on reality than I do, and now, Part II is in its proper home. If you want to read Part I, please visit http://www.dominopower.com/issues/issue200310/00001118001.html. -- David Gewirtz, yer ever-humble Editor-in-Chief

In the first installment of this series, we created a simple framework for the purpose of obtaining an ArrayList of Struts LabelValueBeans to support an html:options tag. To continue with this little microcosm of framework concepts, we're going to go back and tinker with what we've already created and then add another flavor of an implementing class to our portfolio. Finally, we will modify our core components to add some additional functionality that both our original implementing class and our new implementing class will be able to inherit without any further modification to either.

Incremental improvement #1: enhancing what we have
In the final phase of Part I, we created our SimpleOptionListSource class, which took as a data source a String containing a comma-separated list of values. This class extended our abstract OptionListSourceBase class, which implemented our OptionListSource interface. One of the assumptions that we made at the time, in order to keep things as simple as possible, was that both the label and the value would be the same. With just a slight modification and the adoption of a specific syntax, we can enhance this module to accept label/value pairs instead of just values, yet still retain the original functionality for those utilizations that only pass a single value.

Listing 1 contains our new and improved SimpleOptionListSource class, which will now accept both single values and label/value pairs separated by the colon (":") character.

package step.two;

import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.struts.util.LabelValueBean;

/**
* Returns the name/value pairs for the options related to an HTML
* "select" tag.
*
* Source data for this implementation is obtained directly from the
* option list string provided.
*/
public class SimpleOptionListSource extends OptionListSourceBase {
private String optionList = null;

/**
* Constructs a new "SimpleOptionListSource" object using the parameters
* provided.
*
* @param optionList a String containing the comma separated values for
* the option list
*/
public SimpleOptionListSource(String optionList) {
this.optionList = optionList;
}

/**
* Loads the list of available options from the optionList
* String passed to the constructor.
*/
protected void loadOptions() {
ArrayList list = new ArrayList();

StringTokenizer tokens = new StringTokenizer (optionList, ",");
for (int i = 0; i < tokens.countTokens(); i++) {
String thisItem = tokens.nextToken();
LabelValueBean thisOption = new LabelValueBean(thisItem, thisItem);
int x = thisItem.indexOf(":");
if (x > -1) {
String label = thisItem.substring(0, x);
String value = thisItem.substring(x + 1);
thisOption = new LabelValueBean(label, value);
}
list.add(thisOption);
}

setOptions(list);
}
}


1  ·  2  ·  3  ·  4  ·  5  ·  Next »
Other articles you might like
Home > Projects > Reusable Code (5 articles)
   Using a reusable code approach to HTML select option lists, part VI
   Using a reusable code approach to HTML select option lists, part V
   Using a reusable code approach to HTML select option lists, part IV
Get Weekly Email Updates
Subscribe to our regular weekly email newsletter. It's packed with tips, reviews, deep analysis, and the latest news.
 
Recent WebSpherePower Articles
A perfect 10: celebrating 10 years online
You can help bring security and safety back to White House email
Introducing the WebSpherePower RSS feeds
From New Jersey to Palm Bay, Florida
A WebSphere pot o' gold
How Elvis entered the building and CES went out the window
WebSphere Application Server 6: what's it all mean?
WebSpherePower News
Excitement brewing for JavaOne 2010, with or without Google
Large companies ignore data centre advice
Onapsis to Release ERP Vulnerability Testing Suite
Botnet Takedown May Yield Valuable Data
VMware app dev platform gazes beyond SpringSource Java
IBM Claims World's Fastest Chip
'Free Java': InfoWorld's guide to the protest goodies
>> Read all the news
More from the ZATZ journals
Computing Unplugged: Smartphone smarts for a mobile world
David Gewirtz Online: CNN commentary and analysis
DominoPower: It's time for Lotus to double-down on Linux and open source
OutlookPower: The strange case of Outlook losing notes and requiring passwords
-- Advertisement --

SECURE YOUR SITE WITH AN IRONCLAD SSL CERTIFICATE
An IronClad SSL Certificate helps you build an impenetrable fortress around your customer's credit card information. IronClad SSL Certificates are:

  • Fully validated
  • Up to 256-bit encryption
  • One, two, or three year validity (our Turbo SSL Certificates are valid up to 10 years)
  • 99% browser recognition
  • Stringent authentication
  • Around-the-clock customer support

Build trust. Protect your customers. Grow your online business.

Tap here now and be IronClad with SSL tonight.

ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login