org.apache.velocity.tools.struts
Class ValidatorTool

java.lang.Object
  |
  +--org.apache.velocity.tools.struts.ValidatorTool
All Implemented Interfaces:
ViewTool

public class ValidatorTool
extends java.lang.Object
implements ViewTool

View tool that works with Struts Validator to produce client side javascript validation for your forms.

Usage:

 Template example:

 $validator.getJavascript("nameOfYourForm")

 Toolbox configuration:
 <tool>
   <key>validator</key>
   <scope>request</scope>
   <class>org.apache.velocity.tools.struts.ValidatorTool</class>
 </tool>
 

This is an adaptation of the JavascriptValidatorTag from the Struts 1.1 validator library.

Since:
VelocityTools 1.1
Version:
$Revision: 1.6 $ $Date: 2003/11/19 19:35:16 $
Author:
David Winterfeldt, David Graham, Marino A. Jonsson, Nathan Bubna

Nested Class Summary
protected  class ValidatorTool.ValidatorActionComparator
          Inner class for use when creating dynamic javascript
 
Field Summary
protected  javax.servlet.ServletContext app
          A reference to the ServletContext
private  boolean cdata
           
protected  ViewContext context
          A reference to the ViewContext
private  boolean dynamicJavascript
           
private  java.lang.String formName
           
private static java.lang.String HTML_BEGIN_COMMENT
           
private static java.lang.String HTML_END_COMMENT
           
private  boolean htmlComment
           
private  java.lang.String methodName
           
private  int page
           
protected  javax.servlet.http.HttpServletRequest request
          A reference to the HttpServletRequest.
protected  javax.servlet.http.HttpSession session
          A reference to the HttpSession.
private  java.lang.String src
           
private  boolean staticJavascript
           
private  boolean xhtml
           
 
Constructor Summary
ValidatorTool()
          Default constructor.
 
Method Summary
protected  java.util.List createActionList(org.apache.commons.validator.ValidatorResources resources, org.apache.commons.validator.Form form)
          Get List of actions for the given Form.
protected  java.lang.String createMethods(java.util.List actions)
          Creates the JavaScript methods list from the given actions.
 boolean getCdata()
          Returns the cdata setting "true" or "false".
 boolean getDynamicJavascript()
          Gets whether or not to generate the dynamic JavaScript.
protected  java.lang.String getDynamicJavascript(org.apache.commons.validator.ValidatorResources resources, java.util.Locale locale, org.apache.commons.validator.Form form)
          Generates the dynamic JavaScript for the form.
 java.lang.String getFormName()
          Gets the key (form name) that will be used to retrieve a set of validation rules to be performed on the bean passed in for validation.
 boolean getHtmlComment()
          Gets whether or not to delimit the JavaScript with html comments.
 java.lang.String getJavascript()
          Render the JavaScript for to perform validations based on the form name already set the form name via setFormName(java.lang.String) or the form name attribute of the action mapping associated with the current request (if such exists).
 java.lang.String getJavascript(java.lang.String formName)
          Render the JavaScript for to perform validations based on the form name.
protected  java.lang.String getJavascriptBegin(java.lang.String methods)
          Returns the opening script element and some initial javascript.
protected  java.lang.String getJavascriptEnd()
          Returns the closing script element.
protected  java.lang.String getJavascriptStaticMethods(org.apache.commons.validator.ValidatorResources resources)
           
 java.lang.String getMethod()
          Gets the method name that will be used for the Javascript validation method name if it has a value.
private  java.lang.String getNextVar(java.lang.String input)
          The value null will be returned at the end of the sequence.
 int getPage()
          Gets the current page number of a multi-part form.
 java.lang.String getSrc()
          Gets the src attribute's value when defining the html script element.
private  java.lang.String getStartElement()
          Constructs the beginning tag around the javascript.

Parameters:
formName - the key (form name)

getPage

public int getPage()
Gets the current page number of a multi-part form. Only field validations with a matching page numer will be generated that match the current page number. Only valid when the formName attribute is set.

Returns:
the current page number of a multi-part form

setPage

public void setPage(int page)
Sets the current page number of a multi-part form. Only field validations with a matching page numer will be generated that match the current page number. Only valid when the formName attribute is set.

Parameters:
page - the current page number of a multi-part form

getMethod

public java.lang.String getMethod()
Gets the method name that will be used for the Javascript validation method name if it has a value. This overrides the auto-generated method name based on the key (form name) passed in.

Returns:
the method name that will be used for the Javascript validation method

setMethod

public void setMethod(java.lang.String methodName)
Sets the method name that will be used for the Javascript validation method name if it has a value. This overrides the auto-generated method name based on the key (form name) passed in.

Parameters:
methodName - the method name that will be used for the Javascript validation method name

getStaticJavascript

public boolean getStaticJavascript()
Gets whether or not to generate the static JavaScript. If this is set to 'true', which is the default, the static JavaScript will be generated.

Returns:
true to generate the static JavaScript.

setStaticJavascript

public void setStaticJavascript(boolean staticJavascript)
Sets whether or not to generate the static JavaScript. If this is set to 'true', which is the default, the static JavaScript will be generated.

Parameters:
staticJavascript - whether or not to generate the static JavaScript

getDynamicJavascript

public boolean getDynamicJavascript()
Gets whether or not to generate the dynamic JavaScript. If this is set to 'true', which is the default, the dynamic JavaScript will be generated.

Returns:
true to generate the dynamic JavaScript

setDynamicJavascript

public void setDynamicJavascript(boolean dynamicJavascript)
Sets whether or not to generate the dynamic JavaScript. If this is set to 'true', which is the default, the dynamic JavaScript will be generated.

Parameters:
dynamicJavascript - whether or not to generate the dynamic JavaScript

getHtmlComment

public boolean getHtmlComment()
Gets whether or not to delimit the JavaScript with html comments. If this is set to 'true', which is the default, html comments will surround the JavaScript.

Returns:
true if the JavaScript should be delimited with html comments

setHtmlComment

public void setHtmlComment(boolean htmlComment)
Sets whether or not to delimit the JavaScript with html comments. If this is set to 'true', which is the default, html comments will surround the JavaScript.

Parameters:
htmlComment - whether or not to delimit the JavaScript with html comments

getSrc

public java.lang.String getSrc()
Gets the src attribute's value when defining the html script element.

Returns:
the src attribute's value

setSrc

public void setSrc(java.lang.String src)
Sets the src attribute's value (used to include an external script resource) when defining the html script element. The src attribute is only recognized when the formName attribute is specified.

Parameters:
src - the src attribute's value

getCdata

public boolean getCdata()
Returns the cdata setting "true" or "false".

Returns:
boolean - "true" if JavaScript will be hidden in a CDATA section

setCdata

public void setCdata(boolean cdata)
Sets the cdata status.

Parameters:
cdata - The cdata to set

getJavascript

public java.lang.String getJavascript()
                               throws java.lang.Exception
Render the JavaScript for to perform validations based on the form name already set the form name via setFormName(java.lang.String) or the form name attribute of the action mapping associated with the current request (if such exists).

Returns:
the javascript for the current form
Throws:
java.lang.Exception

getJavascript

public java.lang.String getJavascript(java.lang.String formName)
                               throws java.lang.Exception
Render the JavaScript for to perform validations based on the form name.

Parameters:
formName - the key (form name)
Returns:
the Javascript for the specified form
Throws:
java.lang.Exception

getDynamicJavascript

protected java.lang.String getDynamicJavascript(org.apache.commons.validator.ValidatorResources resources,
                                                java.util.Locale locale,
                                                org.apache.commons.validator.Form form)
Generates the dynamic JavaScript for the form.

Parameters:
resources - the validator resources
locale - the locale for the current request
form - the form to generate javascript for
Returns:
the dynamic javascript

createMethods

protected java.lang.String createMethods(java.util.List actions)
Creates the JavaScript methods list from the given actions.

Parameters:
actions - A List of ValidatorAction objects.
Returns:
JavaScript methods.

createActionList

protected java.util.List createActionList(org.apache.commons.validator.ValidatorResources resources,
                                          org.apache.commons.validator.Form form)
Get List of actions for the given Form.

Parameters:
resources - the validator resources
form - the form for which the actions are requested
Returns:
A sorted List of ValidatorAction objects.

getJavascriptBegin

protected java.lang.String getJavascriptBegin(java.lang.String methods)
Returns the opening script element and some initial javascript.

Parameters:
methods - javascript validation methods
Returns:
the opening script element and some initial javascript

getJavascriptStaticMethods

protected java.lang.String getJavascriptStaticMethods(org.apache.commons.validator.ValidatorResources resources)
Parameters:
resources - the validation resources
Returns:
the static javascript methods

getJavascriptEnd

protected java.lang.String getJavascriptEnd()
Returns the closing script element.

Returns:
the closing script element

getNextVar

private java.lang.String getNextVar(java.lang.String input)
The value null will be returned at the end of the sequence. ex: "zz" will return null

Parameters:
input - the string to process
Returns:
the next var

replaceChar

private java.lang.String replaceChar(java.lang.String input,
                                     int pos,
                                     char c)
Replaces a single character in a String

Parameters:
input - the string to process
pos - the position of the caracter to replace
c - the substitute char
Returns:
the input string with the specified char replaced

getStartElement

private java.lang.String getStartElement()
Constructs the beginning


Copyright (c) 2003 Apache Software Foundation