Skip to Main Content
Jakarta Struts Cookbook
book

Jakarta Struts Cookbook

by Bill Siggelkow
February 2005
Intermediate to advanced content levelIntermediate to advanced
528 pages
12h 53m
English
O'Reilly Media, Inc.
Content preview from Jakarta Struts Cookbook

8.1. Reusing Validator Attribute Values

Problem

You want to define, in one place, a common value you can reference wherever needed in a Validator form.

Solution

Define the value as a global or form-set constant in one of your validation documents, as shown in Example 8-3.

Example 8-3. Defining global and form-set validator constants

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
 "-//Apache Software Foundation//DTD Commons Validator Rules 
 Configuration 1.1//EN"
 "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
    <global>
        <constant>
            <constant-name>globalVarName</constant-name>
            <constant-value>globalVarValue</constant-value>
        </constant>
    </global>
    <formset>
        <constant>
            <constant-name>formsetVarName</constant-name>
            <constant-value>formsetVarValue</constant-value>
        </constant>       
        <form name="MyForm">
            <field property="myfield"
                    depends="someRule,anotherRule">
                <var>
                    <var-name>someRule</var-name>
                    <var-value>${globalVarName}</var-value>
                </var>
                <var>
                    <var-name>anotherRule</var-name>
                    <var-value>${formsetVarName}</var-value>
                </var>
            </field>
        </form>
    <formset>
<form-validation>

Discussion

Good developers understand the Don't Repeat Yourself (DRY) principle of software engineering. But if they fail to follow this principle, they could end up all wet. The Validator embraces this guideline, making it an excellent choice for input validation. If you have an attribute value shared by multiple field elements, you can define the value as a named constant. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Jakarta Struts

Programming Jakarta Struts

Chuck Cavaness
Beginning Spring Framework 2

Beginning Spring Framework 2

Bruce Snyder, Sing Li, Anne Horton, Thomas Van de Velde, Naveen Balani, Christian Dupuis
Java Cookbook

Java Cookbook

Ian F. Darwin
Struts 2 in Action

Struts 2 in Action

J. Scott Stanlick, Chad Michael Davis, Donald J. Brown

Publisher Resources

ISBN: 059600771XSupplemental ContentErrata Page