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.
Using a reusable code approach to HTML select option lists, part V (continued)

This is a very simple module, but in order for it to work, it needs two pieces of information:

  • the name of the OptionListSourceFactory that we will be using to produce our OptionListSource object;

  • The name of the OptionListSource object that we need to pass to the factory in order to obtain the source that is applicable to the field being validated.

If you have been following along in this series, you may wonder why we need to pass the name of the factory to the validator since we only created one factory and made no provisions for a collection of factory variations. While that is true, that is just the nature of the shortcut we took to introduce the factory concept. For a complete, production-grade implementation of the factory concept, you should have a factory interface just as we have an OptionListSource interface, and factories should be produced based on a common set of base code contained in an abstract class. Since we elected not to do that, we don't really need to pass the class name of the factory to our validator, but I included that step in here, just because that's really the way it ought to be done.

Once our validator has obtained the option list from the OptionListSource (which it obtained from the factory using the name parameter provided) all that is left for it to do is to iterate through the list of valid values and determine if the user input is on the list. If not, it simply notifies the Struts validation framework and lets the built-in validation features of that framework take care of the rest of the work. All in all, it works out pretty slick thanks to most of the work already being provided by Struts.

Configuring the validator
To make this all work, there are still two more things to do:

  • Define the validator in the Struts validator-rules.xml file;

  • Set up any field validations that you want to have use our new validator.

The entry in the validator-rules.xml is presented here:


<validator name="validValues"
classname="step.five.ValidValuesValidator"
method="validateValidValues"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.valid.values">

<javascript><![CDATA[
function validateValidValues(form) {
return true;
}

]]>
</javascript>

</validator>

Next up is a sample of what a field validation entry in the validation.xml file would look like:


<field property="priority"
depends="required,validValues">
<arg0 key="label.priority"/>
<var>
<var-name>factoryClass</var-name>
<var-value>step.five.OptionListSourceFactory</var-value>
</var>
<var>
<var-name>optionListName</var-name>
<var-value>priority</var-value>
</var>
</field>


« Previous  ·  1  ·  2  ·  3  ·  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 IV
   Using a reusable code approach to HTML select option lists: part III
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 --

EASY DEDICATED AND VIRTUAL DEDICATED SERVERS FOR AS LOW AS $67.99 PER MONTH
Customize and configure your own dedicated server. Simply choose one of our popular plans or select your own Linux or Windows server and plan options.

NO LONG WAITS. Server provisioned within hours.

Tap here now and be up and running with your own server tonight.

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