11.5 The Validator and JSP Custom Tags


Several JSP custom tags included within the Struts tag libraries can be used with the Validator framework. One of the tags is used to generate dynamic JavaScript based on the validation rules. The other tags are part of the core Struts framework and are used both with and without the Validator.

The tags listed in Table 11-4 are generic and can be used with or without the Validator framework, but they all come in handy when using it.

Table 11-4. JSP custom tags that can be used with the Validator

Tag name

Description

Errors

Displays any validation errors found during processing

ErrorsExist

Determines if there were any validation errors

Messages

Displays any messages found during processing

MessagesExist

Determines if there were any messages during processing


The tags in Table 11-4 allow JSP pages to detect and obtain access to messages or errors that were detected in the Struts application. These tags were discussed in more detail in Chapter 8.

11.5.1 Using JavaScript with the Validator

The Validator framework is also capable of generating JavaScript for your Struts application using the same framework as for server-side validation. This is accomplished by using a set of JSP custom tags designed specifically for this purpose.

11.5.1.1 Configuring the validation-rules.xml file for JavaScript

The Validator custom tag called JavascriptValidator is used to generate client-side validation based on a javascript attribute being present within the validator element. Before the JSP custom tag can be used, there must be a javascript element for the validation rule. The following code fragment illustrates the required validation rule that includes a javascript element:

<validator    name="required"   classname="org.apache.struts.util.StrutsValidator"   method="validateRequired"   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.required">   <javascript><![CDATA[     function validateRequired(form) {                                                                              var bValid = true;       var focusField = null;       var i = 0;                                                                                                 var fields = new Array( );                                                                                  oRequired = new required( );                                                                                                                                                                                                     for (x in oRequired) {                                                                                     if ((form[oRequired[x][0]].type == 'text' ||             form[oRequired[x][0]].type ==    'textarea' ||            form[oRequired[x][0]].type == 'select-one' ||             form[oRequired[x][0]].type == 'radio' ||             form[oRequired[x][0]].type == 'password') &&             form[oRequired[x][0]].value == '') {             if (i == 0)              focusField = form[oRequired[x][0]];               fields[i++] = oRequired[x][1];              bValid = false;                                                                                         }                                                                                                      }                                                                                                                                                                                                                              if (fields.length > 0) {         focusField.focus( );         alert(fields.join('\n'));                                                                             }                                                                                                          return bValid;                                                                                          }]]>   </javascript> </validator>

When the JavascriptValidator tag is included in the JSP page, the text from the javascript element is written to the JSP page to provide client-side validation. When the user submits the form, the client-side validation is executed, and any validation rules that fail present messages to the user.

You will need to include the javascript tag with the name of the ActionForm that it's going to validate against:

<html:javascript formName="checkoutForm"/>

The formName attribute is used to look up the set of validation rules to include as JavaScript in the page. You will have to add an onsubmit event handler for the form manually:

<html:form action="getPaymentInfo" onsubmit="return validateCheckoutForm(this);">

When the form is submitted, the validateCheckoutForm( ) JavaScript function will be invoked. The validation rules will be executed, and if one or more rules fail, the form will not be submitted. The javascript tag generates a function with the name validateXXX(), where XXX is the name of the ActionForm. Thus, if your ActionForm is called checkoutForm, the javascript tag will create a JavaScript function called validateCheckoutForm() that executes the validation logic. This is why the onsubmit() event handler called the validateCheckoutForm() function.

By default, the JavascriptValidator tag generates both static and dynamic JavaScript functions. If you would like to include a separate file that contains static JavaScript functions to take advantage of browser caching or to better organize your application, you can use the dynamicJavascript and staticJavascript attributes. By default, both of these are set to true. You can set the staticJavascript attribute to false in your form and include a separate JavaScript page with the dynamicJavascript attribute set to false and the staticJavascript attribute set to true. See the documentation for the JavascriptValidator tag for more information.




Programming Jakarta Struts
Programming Jakarta Struts, 2nd Edition
ISBN: 0596006519
EAN: 2147483647
Year: 2003
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net