📄 dev_validator.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>The Struts User's Guide - Validator Guide</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="David Winterfeldt" name="author" />
<meta content="James Turner" name="author" />
<meta content="Rob Leland" name="author" />
<meta content="Niall Pemberton" name="author" />
<link href="../struts.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="heading">
<a href="http://apache.org/">
<img id="asf_logo_wide" alt="The Apache Project" src="../images/asf_logo_wide.gif" />
</a>
<a href="http://struts.apache.org/">
<img id="struts-logo" alt="Struts Framework" src="../images/struts.gif" />
</a>
</div>
<!--end heading-->
<div id="content">
<div id="menu">
<p>User Guide</p>
<ul>
<li>
<a href="index.html">Table of Contents</a>
</li>
<li>
<a href="preface.html">Preface</a>
</li>
<li>
<a href="introduction.html">Introduction</a>
</li>
<li>
<a href="building_model.html">Model Components</a>
</li>
<li>
<a href="building_view.html">View Components</a>
</li>
<li>
<a href="building_controller.html">Controller Components</a>
</li>
<li>
<a href="configuration.html">Configuration</a>
</li>
<li>
<a href="release-notes.html">Release Notes</a>
</li>
<li>
<a href="installation.html">Installation</a>
</li>
</ul>
<p>Developer Guides</p>
<ul>
<li>
<a href="dev_bean.html">Bean Tags</a>
</li>
<li>
<a href="dev_html.html">HTML Tags</a>
</li>
<li>
<a href="dev_logic.html">Logic Tags</a>
</li>
<li>
<a href="dev_nested.html">Nested Tags</a>
</li>
<li>
<a href="dev_tiles.html">Tiles Tags</a>
</li>
<li>
<a href="dev_util.html">Utilities</a>
</li>
<li>
<a href="dev_validator.html">Validator</a>
</li>
</ul>
<p>Quick Links</p>
<ul>
<li>
<a href="../index.html">Welcome</a>
</li>
<li>
<a href="index.html">User and Developer Guides *</a>
</li>
<li>
<a href="../faqs/index.html">FAQs and HowTos</a>
</li>
</ul>
<div class="authors">
<p>
<strong>Contributors</strong>
</p>
<ul>
<li>David Winterfeldt</li>
<li>James Turner</li>
<li>Rob Leland</li>
<li>Niall Pemberton</li>
</ul>
</div>
</div>
<!--end menu-->
<div id="main">
<h1>Struts Validator Guide</h1>
<h2 id="validator">Struts Validator</h2>
<div class="indent">
<p>
The Struts Validator, in some form, has been available since the
days of Struts 0.5.
It was orignally packaged as a developer contribution.
Later, the core code was moved to the Jakarta Commons and
a Struts specific extension became part of Struts since 1.1.
</p>
<p>
For the convenience of the many developers who have been using
the Struts Validator all along, this document first overviews
the core functionality and then covers the changes and
new functionality added since Struts 1.1.
</p>
<p>
Once you have configured the Validator Plug-In, so that it can load your
Validator Resources you just have to extend
<code>org.apache.struts.validator.action.ValidatorForm</code> instead of
<code>org.apache.struts.action.ActionForm</code>.
Then when the <code>validate</code> method is called, the action's name
attribute from the Struts Configuration is used to load the validations for
the current form.
The form element's <code>name</code> attribute in the
Validator configuration should match the action element's <code>name</code>
attribute.
</p>
<p>
An alternative approach is to use the action mapping path attribute.
In this case, you extend the ValidatorActionForm instead of the ValidatorForm.
The ValidatorActionForm uses the action element's <code>path</code>
attribute from the Struts configuration which should match the form element's
<code>name</code> attribute in the Validator configuration.
</p>
<p>
Then a separate action mapping can be defined for each page in a multi-page
form, and the validation form can be associated with the action rather than
a page number (as shown in the example of a multi-page form in the validator
example).
</p>
</div>
<h2 id="i18n">Internationalization</h2>
<div class="indent">
<p>
Each validator form is grouped within a <code>FormSet</code> element in
the Validator configuration file.
The <code>FormSet</code> has language, country, and variant attributes that
correspond with the <code>java.util.Locale</code> class.
If these attributes are not specified, the <code>FormSet</code> will be set
to the default locale.
A <code>FormSet</code> can also have constants associated with it.
On the same level as a <code>FormSet</code> there can be a global element
which can also have constants and have validator actions that perform
validations.
</p>
<p>
<strong>Note</strong>: You must declare a default <code>FormSet</code> without
internationalization before your internationalized <code>FormSet</code>s. This
allows the Validator to fall back to the default version if no locale is found.
</p>
<p>
The default error message for a pluggable validator can be overriden with
the <code>msg</code> element.
So instead of using the <code>msg</code> attribute for the mask validator
to generate the error message the <code>msg</code> attribute from the
field will be used if the name of the field's name attribute matches the
validator's name attribute.
</p>
<p>
The arguments for error messages can be set with the arg0-arg3 elements.
If the arg0-arg3 elements' name attribute isn't set, it will become the
default arg value for the different error messages constructed.
If the name attribute is set, you can specify the argument for a specific
pluggable validator and then this will be used for constructing the error
message.
</p>
<pre>
<code>
<field
property="lastName"
depends="required,mask">
<msg
name="mask"
key="registrationForm.lastname.maskmsg"/>
<arg0 key="registrationForm.lastname.displayname"/>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z]*$</var-value>
</var>
</field>
</code>
</pre>
<p>
By default the arg0-arg3 elements will try to look up the <code>key</code>
attribute in the message resources.
If the resource attribute is set to false, it will pass in the value directly
without retrieving the value from the message resources.
</p>
<p>
Note that since Struts 1.1, you must explicitly define your message
resource in any module that is going to use the Validator, due to a problem
accessing the top-level resource. This only effects applications which are
using modules.
</p>
<pre>
<code>
<field
property="integer"
depends="required,integer,intRange">
<arg0 key="typeForm.integer.displayname"/>
<arg1
name="intRange"
key="${var:min}"
resource="false"/>
<arg2
name="intRange"
key="${var:max}"
resource="false"/>
<var>
<var-name>min</var-name>
<var-value>10</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>20</var-value>
</var>
</field>
</code>
</pre>
</div>
<h2 id="builtin">Standard Built In Validations</h2>
<div class="indent">
<p>
Validator ships with a set of pre-defined validators, as follows:
</p>
<ul>
<li>
<strong>required</strong> - mandatory field validation. Has no variables.
<pre>
<code>
<field property="name" depends="required">
<arg0 key="customer.name"/>
</field>
</code>
</pre>
</li>
<li>
<strong>requiredif</strong> - field dependant validator
<pre>Deprecated, use validwhen.</pre>
</li>
<li>
<strong>validwhen</strong> - validator for checking one field against another.
<pre>see later section titled <strong>Designing "Complex Validations with validwhen"</strong>.</pre>
</li>
<li>
<strong>minlength</strong> - validate input data isn't less than a specified minimum length.
Requires a <code>minlength</code> variable.
<pre>
<code>
<field property="name" depends="required,minlength">
<arg0 key="customer.name"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<var><var-name>minlength</var-name><var-value>3</var-value></var>
</field>
</code>
</pre>
</li>
<li>
<strong>maxlength</strong> - validate input data doesn't exceed a specified maximum length.
Requires a <code>maxlength</code> variable.
<pre>
<code>
<field property="name" depends="required,maxlength">
<arg0 key="customer.name"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<var><var-name>maxlength</var-name><var-value>30</var-value></var>
</field>
</code>
</pre>
</li>
<li>
<strong>mask</strong> - validate format according to a regular expression.
Requires a <code>mask</code> variable to specify the regular expression.
Since version 1.1, the regular expression must start
with a <code>^</code> and end with a <code>$</code> (see example below).
<pre>
<code>
<field property="name" depends="required,mask">
<msg name="mask" key="registrationForm.lastname.maskmsg"/>
<arg0 key="registration.name"/>
<var><var-name>mask</var-name><var-value>^[a-zA-Z]*$</var-value></var>
</field>
</code>
</pre>
</li>
<li>
<strong>byte</strong> - validates that a field can be converted to a Byte.
<pre>
<code>
<field property="age" depends="byte">
<arg0 key="employee.age"/>
</field>
</code>
</pre>
</li>
<li>
<strong>short</strong> - validates that a field can be converted to a Short.
<pre>
<code>
<field property="productnumber" depends="short">
<arg0 key="order.prodno"/>
</field>
</code>
</pre>
</li>
<li>
<strong>integer</strong> - validates that a field can be converted to an Integer.
<pre>
<code>
<field property="ordernumber" depends="integer">
<arg0 key="order.number"/>
</field>
</code>
</pre>
</li>
<li>
<strong>long</strong> - validates that a field can be converted to a Long.
<pre>
<code>
<field property="ordernumber" depends="long">
<arg0 key="order.number"/>
</field>
</code>
</pre>
</li>
<li>
<strong>float</strong> - validates that a field can be converted to a Float.
<pre>
<code>
<field property="amount" depends="float">
<arg0 key="sale.amount"/>
</field>
</code>
</pre>
</li>
<li>
<strong>double</strong> - validates that a field can be converted to a Double.
<pre>
<code>
<field property="amount" depends="double">
<arg0 key="sale.amount"/>
</field>
</code>
</pre>
</li>
<li>
<strong>date</strong> - validates that a field can be converted to a Date.
This validator uses <code>java.text.SimpleDateFormat</code> to parse the date and
optionally either a <code>datePattern</code> or <code>datePatternStrict</code> variable can be used.
If no pattern is specified the default short date format is assumed. The difference
between using the <code>datePatternStrict</code> and <code>datePattern</code> variables
is that <code>datePatternStrict</code> checks additionally that the input data is the same
length as the pattern specified (so for example 1/1/2004 would fail with a pattern of <code>MM/dd/yyyy</code>).
<pre>
<code>
<field property="saledate" depends="required,date">
<arg0 key="myForm.saledate"/>
<var><var-name>datePattern</var-name><var-value>MM/dd/yyyy</var-value></var>
</field>
</code>
</pre>
<pre>
<code>
<field property="saledate" depends="required,date">
<arg0 key="sale.orderdate"/>
<var><var-name>datePatternStrict</var-name><var-value>MM/dd/yyyy</var-value></var>
</field>
</code>
</pre>
</li>
<li>
<strong>range</strong> - validate number range.
<pre>Deprecated, use intRange, floatRange or doubleRange.</pre>
</li>
<li>
<strong>intRange</strong> - validates that an integer field is within a specified range.
Requires <code>min</code> and <code>max</code> variables to specify the range.
This validator depends on the <code>integer</code> validator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -