﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-freefly-文章分类-struts</title><link>http://www.blogjava.net/freefly/category/10621.html</link><description>一门技术，如果不能讲出来，那么就是没有理解，如果不能很好的讲出来,那么就是理解不够透彻！</description><language>zh-cn</language><lastBuildDate>Mon, 19 Nov 2007 19:33:58 GMT</lastBuildDate><pubDate>Mon, 19 Nov 2007 19:33:58 GMT</pubDate><ttl>60</ttl><item><title>Check Your Form with Validator</title><link>http://www.blogjava.net/freefly/articles/44032.html</link><dc:creator>freefly</dc:creator><author>freefly</author><pubDate>Sat, 29 Apr 2006 08:45:00 GMT</pubDate><guid>http://www.blogjava.net/freefly/articles/44032.html</guid><wfw:comment>http://www.blogjava.net/freefly/comments/44032.html</wfw:comment><comments>http://www.blogjava.net/freefly/articles/44032.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/freefly/comments/commentRss/44032.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/freefly/services/trackbacks/44032.html</trackback:ping><description><![CDATA[<p dir="rtl"><font style="background-color: #ffffff" color="#000000">Check Your Form with Validator </font></p>
<p><font style="background-color: #ffffff" color="#000000">By James Holmes </font></p>
<p><font style="background-color: #ffffff" color="#000000">Simplify Struts development with Validator's rich set of built-in validations. </font></p>
<p><font style="background-color: #ffffff" color="#000000">One major benefit of the Struts framework is its built-in interface for performing data validations on incoming form data. If any validations fail, the application redisplays the HTML form so that the invalid data can be corrected. Otherwise, processing continues. The Struts framework's simple validation interface alleviates much of the headache associated with handling data validation, allowing you to focus on validation code and not on the mechanics of capturing data and redisplaying incomplete or invalid data. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Struts' built-in validation interface, however, has its shortcomings. Often, for example, validation code is heavily duplicated throughout an application, because many fields require the same validation logic. Any change in the validation logic for similar fields requires code changes in several places as well as recompilation of the affected code. To solve this problem and to enhance Struts' validation interface, the Validator framework was created as a third-party add-on to Struts. Validator was later integrated into the core Struts code base and has since been detached from Struts and is now a standalone Jakarta Commons project. Although Validator is an independent framework again, it still comes packaged and seamlessly integrated with Struts. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Validator Overview </font></p>
<p><font style="background-color: #ffffff" color="#000000">Without Validator, you have to code all of your form data validations into the validate( ) methods of your Form Bean objects. Each Form Bean field on which you want to perform a validation requires you to code logic to do so. Additionally, you have to write code that stores error messages for validations that fail. </font></p>
<p><font style="background-color: #ffffff" color="#000000">With Validator you don't have to write any code in your Form Beans for validations or storing error messages. Instead, your Form Beans extend one of Validator's ActionForm subclasses that provide this functionality for you. </font></p>
<p><font style="background-color: #ffffff" color="#000000">The Validator framework is set up as a pluggable system of validation routines that can be applied to Form Beans. Each validation routine is simply a Java method responsible for performing a specific type of validation and can either pass or fail. By default, Validator comes packaged with several useful validation routines that will satisfy most validation scenarios. However, if you need a validation not provided by the Validator framework, you can create your own custom validation routine and plug it into the framework. Additionally, Validator supports both server-side and client-side (JavaScript) validations, whereas Form Beans provide only a server-side validation interface. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Validator uses two XML configuration files to determine which validation routines should be installed and how they should be applied for a given application, respectively. The first configuration file, validator-rules.xml, declares the validation routines that should be plugged into the framework and provides logical names for each of the validations. The validator-rules.xml file also defines client-side JavaScript code for each validation routine. Validator can be configured to send this JavaScript code to the browser so that validations are performed on the client side as well as on the server side. </font></p>
<p><font style="background-color: #ffffff" color="#000000">The second configuration file, validation.xml, defines which validation routines should be applied to which Form Beans. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Using the Validator framework involves enabling the Validator plug-in, configuring Validator's two configuration files, and creating Form Beans that extend the Validator's ActionForm subclasses. The following sections explain in detail how to configure and use Validator. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Enabling the Validator Plug-in </font></p>
<p><font style="background-color: #ffffff" color="#000000">Although the Validator framework comes packaged with Struts, Validator is not enabled by default. To enable Validator, add the following plug-in definition to your application's struts-config.xml file. </font><br />
<font style="background-color: #ffffff" color="#000000">&lt;!-- Validator Configuration --&gt;<br />
&lt;plug-in className="org.apache.struts<br />
.validator.ValidatorPlugIn"&gt;<br />
&nbsp; &lt;set-property property="pathnames"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="/technology/WEB-INF/<br />
&nbsp; validator-rules.xml, /WEB-INF/<br />
&nbsp; validation.xml"/&gt;<br />
&lt;/plug-in&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">This definition tells Struts to load and initialize the Validator plug-in for your application. Upon initialization, the plug-in loads the comma-delimited list of Validator config files specified by the pathnames property. Each config file's path should be specified by use of a Web application-relative path, as shown in the previous example. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Note that your application's struts-config.xml file must conform to the Struts Configuration Document Type Definition (DTD), which specifies the order in which elements are to appear in the file. Therefore, you have to put the Validator plug-in definition into the proper place in the file. The easiest way to ensure that you are properly ordering elements in the file is to use a tool, such as Struts Console, that automatically formats your configuration file so that it conforms to the DTD. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Configuring validator-rules.xml </font></p>
<p><font style="background-color: #ffffff" color="#000000">The Validator framework is set up as a pluggable system whose validation routines are simply Java methods that are plugged into the system to perform specific validations. The validator-rules.xml file declaratively plugs in the validation routines Validator uses for performing validations. Struts example applications come packaged with preconfigured copies of this file. Under most circumstances, you will not need to modify the preconfigured copies unless you are adding your own custom validations to the framework. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Listing 1 is a sample validator-rules.xml file that illustrates how validation routines are plugged into Validator. Each validation routine in the validator-rules.xml file has its own definition that is declared with a validator tag, which is used to assign a logical name to the routine, using the name attribute, and to specify the class and method for the routine. The routine's logical name is used by other routines in this file as well as by validation definitions in the validation.xml file to refer to the routine. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Note that the validator tag encapsulates a javascript tag, which is used to define client-side JavaScript code for performing the same validation on the client side as on the server side. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Included Validations </font></p>
<p><font style="background-color: #ffffff" color="#000000">By default, Validator comes packaged with several basic validation routines you can use to handle most validation scenarios. These routines have logical names, such as required (for required values), CreditCard (for credit card number values), email (for e-mail address values), and so on. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Creating Form Beans </font></p>
<p><font style="background-color: #ffffff" color="#000000">To use Validator, your application's Form Beans have to subclass one of Validator's ActionForm subclasses instead of ActionForm itself. Validator's ActionForm subclasses provide an implementation for ActionForm's validate( ) method that hooks into the Validator framework. Instead of hard-coding validations into the validate( ) method, you simply omit the method altogether, because Validator provides the validation code for you. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Parallel to the core functionality provided by Struts, Validator gives you two paths to choose from when creating Form Beans. The first path you can choose is to create a concrete Form Bean object like the following: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">package com.jamesholmes.minihr;</font> </p>
<p><font style="background-color: #ffffff" color="#000000">import org.apache.struts.validator<br />
.ValidatorForm;</font> </p>
<p><font style="background-color: #ffffff" color="#000000">public class LogonForm extends ValidatorForm {<br />
&nbsp; private String username;<br />
&nbsp; private String password;<br />
&nbsp; <br />
&nbsp; public String getUsername() {<br />
&nbsp;&nbsp;&nbsp; return username;<br />
&nbsp; }<br />
&nbsp; <br />
&nbsp; public void setUsername(String <br />
username) {<br />
&nbsp;&nbsp;&nbsp; this.username = username;<br />
&nbsp; }</font> </p>
<p><font style="background-color: #ffffff" color="#000000">&nbsp; public String getPassword() {<br />
&nbsp;&nbsp;&nbsp; return password;<br />
&nbsp; }<br />
public void setPassword(String <br />
password) {<br />
&nbsp;&nbsp;&nbsp; this.password = password;<br />
&nbsp; }<br />
}</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">This class is similar to one you would create if you were not using Validator, but this class extends ValidatorForm instead of ActionForm. This class also does not provide an implementation for ActionForm's empty reset( ) and validate( ) methods, because ValidatorForm does. </font></p>
<p><font style="background-color: #ffffff" color="#000000">You configure this concrete Form Bean in the struts-config.xml file the same way you would a regular Form Bean: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;form-beans&gt;<br />
&nbsp; &lt;form-bean name="logonForm"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type="com.jamesholmes<br />
&nbsp; .minihr.LogonForm"/&gt;<br />
&lt;/form-beans&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">The logical name given to the concrete Form Bean with the form tag's name attribute is the name you use when defining validations in the validation.xml file, as shown here. </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;!DOCTYPE form-validation <br />
PUBLIC "-//Apache Software Foundation//<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DTD Commons Validator Rules<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Configuration 1.0//EN"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "</font> <a href="http://jakarta.apache.org/"><font style="background-color: #ffffff" color="#000000">http://jakarta.apache.org/</font> </a><br />
<font style="background-color: #ffffff" color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; commons/dtds/validator_1_0.dtd"&gt;</font> </p>
<p><font style="background-color: #ffffff" color="#000000">&lt;form-validation&gt;<br />
&nbsp; &lt;formset&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;form name="logonForm"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field property="username" <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; depends="required"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;arg0 key="prompt.username"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/field&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/form&gt;<br />
&nbsp; &lt;/formset&gt;<br />
&lt;/form-validation&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">Validator uses the value of the form tag's name attribute to match validation definitions to the name of the Form Bean to which they should be applied. </font></p>
<p><font style="background-color: #ffffff" color="#000000">The second path you can choose when creating your Form Bean is to define a dynamic Form Bean in the struts-config.xml file, as follows: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;form-beans&gt;<br />
&nbsp; &lt;form-bean name="logonForm"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type="org.apache<br />
.struts.validator.DynaValidatorForm"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;form-property name="username"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type="java.lang.String"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;form-property name="password"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type="java.lang.String"/&gt;<br />
&nbsp; &lt;/form-bean&gt;<br />
&lt;/form-beans&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">Dynamic Form Beans do not require you to create concrete Form Bean objects; instead, you define the properties your Form Bean should have and their types, and Struts dynamically creates the Form Bean for you. Validator allows you to use this concept just as you would with core Struts. The only difference with Validator is that you specify that your Form Bean is of type org.apache.struts.validator.DynaValidatorForm instead of org.apache.struts.action.DynaActionForm. </font></p>
<p><font style="background-color: #ffffff" color="#000000">The logical name given to dynamic Form Beans is the name you use when defining validations in the validation.xml file. Validator uses the matching names to tie the validations to the Form Bean. </font></p>
<p><font style="background-color: #ffffff" color="#000000">In addition to the two standard options for creating Form Beans, Validator provides an advanced feature for tying multiple validation definitions to one Form Bean definition. When you use validatorForm- or DynaValidatorForm-based Form Beans, Validator uses the logical name for the Form Bean from the struts-config.xml file to map the Form Bean to validation definitions in the validation.xml file. This mechanism is great in most cases, but in some scenarios, Form Beans are shared among multiple actions. One action may use all of the Form Bean's fields, and another action may use only a subset of the fields. Because validation definitions are tied to the Form Bean, the action that uses only a subset of the fields has no way of bypassing validations for the unused fields. When the Form Bean is validated, it generates error messages for the unused fields, because Validator has no way of knowing not to validate the unused fields; it simply sees them as missing or invalid. </font></p>
<p><font style="background-color: #ffffff" color="#000000">To solve this problem, Validator provides two additional ActionForm subclasses that allow you to tie validations to actions instead of to Form Beans. That way you can specify which validations to apply to the Form Bean based on which action is using the Form Bean. For concrete Form Beans, you subclass org.apache.struts.validator.ValidatorActionForm, as follows: </font></p>
<p><font style="background-color: #ffffff" color="#000000"></font>&nbsp;</p>
<p><font style="background-color: #ffffff" color="#000000">public class AddressForm extends ValidatorActionForm {<br />
&nbsp; ...<br />
}</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">For dynamic Form Beans, you specify a type of org.apache.struts.validator.DynaValidatorActionForm for your Form Bean definition in the struts-config.xml file, as follows: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;form-bean name="addressForm"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type="org.apache.struts<br />
.validator.DynaValidatorActionForm"&gt;<br />
&nbsp; ...<br />
&lt;/form-bean&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">Inside your validation.xml file, you map a set of validations to an action path instead of to a Form Bean name, because if you have two actions defined, Create Address and Edit Address, which use the same Form Bean, each will have a unique action path, as follows: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;action-mappings&gt;<br />
&nbsp; &lt;action path="/technology/createAddress"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type="com.jamesholmes<br />
&nbsp; .minihr.CreateAddressAction"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name="addressForm"/&gt;<br />
&nbsp; &lt;action path="/technology/editAddress"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type="com.jamesholmes<br />
&nbsp; .minihr.EditAddressAction"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name="addressForm"/&gt;<br />
&lt;/action-mappings&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">The following validation.xml file snippet shows two sets of validations that are intended for the same Form Bean but are distinguished by different action paths: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;formset&gt;<br />
&nbsp; &lt;form name="/technology/createAddress"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;field property="city"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; depends="required"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;arg0 key="prompt.city"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/field&gt;<br />
&nbsp; &lt;/form&gt;<br />
&nbsp; &lt;form name="/technology/editAddress"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;field property="state"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; depends="required"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;arg0 key="prompt.state"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/field&gt;<br />
&nbsp; &lt;/form&gt;<br />
&lt;/formset&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">Because your Form Bean subclasses either ValidatorActionForm or DynaValidatorActionForm, Validator knows to use an action path instead of the Form Bean's logical name to find validations for the Form Bean. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Configuring validation.xml </font></p>
<p><font style="background-color: #ffffff" color="#000000">The validation.xml file is used to declare sets of validations that should be applied to Form Beans. Each Form Bean you want to validate has its own definition in this file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields. The following is a sample validation.xml file that illustrates how validations are defined: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;!DOCTYPE form-validation <br />
PUBLIC "-//Apache Software Foundation//<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DTD Commons Validator Rules<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Configuration 1.0//EN"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "</font> <a href="http://jakarta.apache.org/"><font style="background-color: #ffffff" color="#000000">http://jakarta.apache.org/</font> </a><br />
<font style="background-color: #ffffff" color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; commons/dtds/validator_1_0.dtd"&gt;</font> </p>
<p><font style="background-color: #ffffff" color="#000000">&lt;form-validation&gt;<br />
&nbsp; &lt;formset&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;form name="logonForm"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field property="username"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; depends="required"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;arg0 key="prompt.username"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/field&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field property="password"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; depends="required"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;arg0 key="prompt.password"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/field&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/form&gt;<br />
&nbsp; &lt;/formset&gt;<br />
&lt;/form-validation&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">The first element in the validation.xml file is the form-validation element. This element is the master element for the file and is defined only once. Inside the form-validation element, you define form-set elements that encapsulate multiple form elements. Generally, you define only one form-set element in your file, but you would use a separate one for each locale if you were internationalizing validations. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Each form element uses the name attribute to associate a name with the set of field validations it encompasses. Validator uses this logical name to map the validations to a Form Bean defined in the struts-config.xml file. Based on the type of Form Bean being validated, Validator attempts to match the name to either a Form Bean's logical name or an action path. Inside the form element, field elements define the validations to apply to specified Form Bean fields. The field element's property attribute corresponds to the name of a field in the specified Form Bean. The depends attribute specifies the logical names of validation routines from the validator-rules.xml file that should be applied to the field. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Configuring ApplicationResources.properties </font></p>
<p><font style="background-color: #ffffff" color="#000000">Validator uses Struts' Resource Bundle mechanism for externalizing error messages. Instead of having hard-coded error messages in the framework, Validator allows you to specify a key to a message in the ApplicationResources.properties file that should be returned if a validation fails. Each validation routine in the validator-rules.xml file specifies an error message key with the validator tag's msg attribute, as follows: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;validator name="required"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; classname="org.apache<br />
.struts.validator.FieldChecks"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method="validateRequired"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; methodParams="java.lang<br />
.Object, org.apache.commons.validator<br />
.ValidatorAction, org.apache.commons<br />
.validator.Field, org.apache.struts<br />
.action.ActionErrors, javax.servlet<br />
.http.HttpServletRequest"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg="errors.required"&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">If the validation fails when it is run, the message corresponding to the key specified by the msg attribute will be returned. </font></p>
<p><font style="background-color: #ffffff" color="#000000">The following snippet shows the default set of validation error messages from the ApplicationResources.properties file that comes prepackaged with Struts example applications. Each message key corresponds to those specified by the validation routines in the validator-rules.xml file, which also comes prepackaged with Struts example applications. </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000"># Error messages for Validator framework validations<br />
errors.required={0} is required.<br />
errors.minlength={0} cannot be less than {1} characters.<br />
errors.maxlength={0} cannot be greater than {2} characters.<br />
errors.invalid={0} is invalid.<br />
errors.byte={0} must be a byte.<br />
errors.short={0} must be a short.<br />
errors.integer={0} must be an integer.<br />
errors.long={0} must be a long.0.&nbsp;&nbsp; errors.float={0} must be a float.<br />
errors.double={0} must be a double.<br />
errors.date={0} is not a date.<br />
errors.range={0} is not in the range {1} through {2}.<br />
errors.creditcard={0} is not a valid credit card number.<br />
errors.email={0} is an invalid e-mail address.</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">Note that each message has placeholders, in the form {0}, {1}, or {2}. At runtime the placeholders are replaced by another value such as the name of the field being validated. This feature is especially useful in allowing you to create generic validation error messages that can be reused for several different fields. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Take, for example, the required validation's error message, errors.required, shown here: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">errors.required={0} is required.</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">When you use the required validation in the validation.xml file, you have to define the value that should be used to replace {0} in the error message, as shown here. </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;form name="auctionForm"&gt;<br />
&nbsp; &lt;field property="bid" depends="required"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;arg0 key="prompt.bid"/&gt;<br />
&nbsp; &lt;/field&gt;<br />
&lt;/form&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">Error messages can have up to four placeholders: {0} through {3}. These placeholders are known as arg0 through arg3, respectively, and you can specify them by using the arg0 - arg3 tags. In the example above, the arg0 tag specifies the value that should be used to replace the {0} placeholder. This tag's key attribute specifies a message key from the ApplicationResources.properties file, such as the following, whose value is used as the replacement for the placeholder: Next Steps </font></p>
<p><font style="background-color: #ffffff" color="#000000">READ<br />
more about Validator<br />
jakarta.apache.org/commons/validator </font></p>
<p><font style="background-color: #ffffff" color="#000000">more about Struts Console<br />
</font><a href="http://www.jamesholmes.com/struts"><font style="background-color: #ffffff" color="#000000">www.jamesholmes.com/struts</font> </a><font style="background-color: #ffffff" color="#000000"><br />
&nbsp;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">prompt.bid=Auction Bid</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">Using a message key for the placeholder value frees you from having to hard-code the replacement value over and over in the validation.xml file. However, if you don't want to use the Resource Bundle key/value mechanism for specifying placeholder values, you can explicitly specify the placeholder value by using the following syntax for the arg0 tag. </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;arg0 key="Auction Bid" resource="false"/&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">In this example, the resource attribute is set to false to instruct Validator that the value specified with the key attribute should be taken as the literal placeholder value and not as a key for a message in the ApplicationResources.properties file. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Enabling Client-Side Validations </font></p>
<p><font style="background-color: #ffffff" color="#000000">Besides providing a framework for simplifying server-side form data validations, Validator provides an easy-to-use mechanism for performing client-side validations. Each validation routine defined in the validator-rules.xml file optionally specifies JavaScript code that can be run in the browser (on the client side) to perform the same validations that take place on the server side. When run on the client side, the validations do not allow the form to be submitted until they have all passed. </font></p>
<p><font style="background-color: #ffffff" color="#000000">To enable client-side validation, you have to place the Struts HTML Tag Library's javascript tag in each JSP that needs validation, as follows: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;html:javascript formName="logonForm"/&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">The javascript tag requires you to use the formName attribute to specify the name of a form definition from the validation.xml file for which you want validations performed, as follows: </font></p>
<p><br />
<font style="background-color: #ffffff" color="#000000">&lt;form name="logonForm"&gt;<br />
&nbsp; &lt;field property="username"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; depends="required"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;arg0 key="prompt.username"/&gt;<br />
&nbsp; &lt;/field&gt;<br />
&nbsp; &lt;field property="password"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; depends="required"&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;arg0 key="prompt.password"/&gt;<br />
&nbsp; &lt;/field&gt;<br />
&lt;/form&gt;</font> </p>
<p><br />
<font style="background-color: #ffffff" color="#000000">All the validations you have specified for the form definition to run on the server side are also run on the client side. Because client-side validation is performed with JavaScript, there are ways of circumventing it. To ensure that validations are always run, Validator performs the validations on the server side, whether or not you have chosen to enable client-side validation. </font></p>
<p><font style="background-color: #ffffff" color="#000000">Conclusion </font></p>
<p><font style="background-color: #ffffff" color="#000000">The Validator framework adds significant value to the core Struts framework, by providing a configurable system for applying form data validations. You can save time and simplify your Struts application development by using the Validator framework with your application<br />
</font><a href="http://www.oracle.com/technology/oramag/oracle/04-jan/o14dev_struts.html"></a></p>
<img src ="http://www.blogjava.net/freefly/aggbug/44032.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/freefly/" target="_blank">freefly</a> 2006-04-29 16:45 <a href="http://www.blogjava.net/freefly/articles/44032.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>attribute's site</title><link>http://www.blogjava.net/freefly/articles/43989.html</link><dc:creator>freefly</dc:creator><author>freefly</author><pubDate>Sat, 29 Apr 2006 05:14:00 GMT</pubDate><guid>http://www.blogjava.net/freefly/articles/43989.html</guid><wfw:comment>http://www.blogjava.net/freefly/comments/43989.html</wfw:comment><comments>http://www.blogjava.net/freefly/articles/43989.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/freefly/comments/commentRss/43989.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/freefly/services/trackbacks/43989.html</trackback:ping><description><![CDATA[1.<br />LoginAction.java <br />public class LoginAction extends Action {<br />    public ActionForward execute(<br />                             ActionMapping mapping,<br />                             ActionForm form,<br />                             HttpServletRequest request, <br />                             HttpServletResponse response) <br />                                    throws Exception { <br />        String username = ((UserForm) form).getUsername(); <br />        String password = ((UserForm) form).getPassword(); <br />        <br />        <font style="BACKGROUND-COLOR: #ff1493">request.setAttribute("username", username);<br /></font>        <br />        if(username.equals("freefly") &amp;&amp; <br />                  password.equals("whl")) {<br />            return mapping.findForward("helloUser"); <br />        }<br />        <br />        return mapping.findForward("loginFail"); <br />    } <br />}<br />2.hello.jsp <pre>&lt;html&gt; <br />&lt;head&gt; <br />&lt;title&gt;Hello, ${username} !&lt;/title&gt; <br />&lt;/head&gt; <br />&lt;body&gt;<br /><font style="BACKGROUND-COLOR: #ff1493">&lt;H1&gt;Hello, ${username} !&lt;/H1&gt;</font><br />     &lt;H1&gt;This is your secret gift!!&lt;/H1&gt;<br />&lt;/body&gt; <br />&lt;/html&gt;</pre><br /><img src ="http://www.blogjava.net/freefly/aggbug/43989.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/freefly/" target="_blank">freefly</a> 2006-04-29 13:14 <a href="http://www.blogjava.net/freefly/articles/43989.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>