org.apache.struts.taglib.html

Class JavascriptValidatorTag

public class JavascriptValidatorTag extends BodyTagSupport

Custom tag that generates JavaScript for client side validation based on the validation rules loaded by the ValidatorPlugIn defined in the struts-config.xml file.

Since: Struts 1.1

Version: $Rev: 383908 $ $Date: 2006-03-07 15:33:42 +0000 (Tue, 07 Mar 2006) $

Field Summary
static ComparatoractionComparator
A Comparator to use when sorting ValidatorAction objects.
protected Stringbundle
The servlet context attribute key for our resources.
protected Stringcdata
Hide JavaScript methods in a CDATA section for XHTML when "true".
protected static LocaledefaultLocale
The default locale on our server.
protected StringdynamicJavascript
The dynamic JavaScript objects will only be generated if this is set to "true".
protected StringformName
The name of the form that corresponds with the action name in struts-config.xml.
protected StringhtmlComment
The JavaScript methods will enclosed with html comments if this is set to "true".
protected static StringHTML_BEGIN_COMMENT
The start of the HTML comment hiding JavaScript from old browsers.
protected static StringHTML_END_COMMENT
The end of the HTML comment hiding JavaScript from old browsers.
protected StringjsFormName
formName is used for both Javascript and non-javascript validations.
protected static StringlineEnd
The line ending string.
protected StringmethodName
This will be used as is for the JavaScript validation method name if it has a value.
protected intpage
The current page number of a multi-part form.
protected booleanscriptLanguage
Include language attribute in the <script> element.
protected Stringsrc
The src attribute for html script element (used to include an external script resource).
protected StringstaticJavascript
The static JavaScript methods will only be printed if this is set to "true".
Method Summary
ListcreateActionList(ValidatorResources resources, Form form)
Get List of actions for the given Form.
StringcreateDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form)
Generates the dynamic JavaScript for the form.
StringcreateMethods(List actions, boolean stopOnError)
Creates the JavaScript methods list from the given actions.
intdoStartTag()
Render the JavaScript for to perform validations based on the form name.
StringescapeJavascript(String str)

Backslash-escapes the following characters from the input string: ", ', \, \r, \n.

This method escapes characters that will result in an invalid Javascript statement within the validator Javascript.

StringescapeQuotes(String in)
StringgetBundle()
Sets the servlet context attribute key for our resources.
StringgetCdata()
Returns the cdata setting "true" or "false".
StringgetDynamicJavascript()
Gets whether or not to generate the dynamic JavaScript.
StringgetFormName()
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.
StringgetHtmlComment()
Gets whether or not to delimit the JavaScript with html comments.
protected StringgetJavascriptBegin(String methods)
Returns the opening script element and some initial javascript.
protected StringgetJavascriptEnd()
Returns the closing script element.
protected StringgetJavascriptStaticMethods(ValidatorResources resources)
StringgetJsFormName()
StringgetMethod()
Gets the method name that will be used for the Javascript validation method name if it has a value.
intgetPage()
Gets the current page number of a multi-part form.
booleangetScriptLanguage()
Gets whether or not the <script> element will include the language attribute.
StringgetSrc()
Gets the src attribute's value when defining the html script element.
StringgetStaticJavascript()
Gets whether or not to generate the static JavaScript.
booleanisXhtml()
Returns true if this is an xhtml page.
voidrelease()
Release any acquired resources.
protected StringrenderJavascript()
Returns fully rendered JavaScript.
protected StringrenderStartElement()
Constructs the beginning <script> element depending on XHTML status.
voidsetBundle(String bundle)
Gets the servlet context attribute key for our resources.
voidsetCdata(String cdata)
Sets the cdata status.
voidsetDynamicJavascript(String dynamicJavascript)
Sets whether or not to generate the dynamic JavaScript.
voidsetFormName(String formName)
Sets 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.
voidsetHtmlComment(String htmlComment)
Sets whether or not to delimit the JavaScript with html comments.
voidsetJsFormName(String jsFormName)
voidsetMethod(String methodName)
Sets the method name that will be used for the Javascript validation method name if it has a value.
voidsetPage(int page)
Sets the current page number of a multi-part form.
voidsetScriptLanguage(boolean scriptLanguage)
Sets whether or not the <script> element will include the language attribute.
voidsetSrc(String src)
Sets the src attribute's value when defining the html script element.
voidsetStaticJavascript(String staticJavascript)
Sets whether or not to generate the static JavaScript.
booleanstopOnError(ModuleConfig config)
Determines if validations should stop on an error.

Field Detail

actionComparator

private static final Comparator actionComparator
A Comparator to use when sorting ValidatorAction objects.

bundle

protected String bundle
The servlet context attribute key for our resources.

cdata

protected String cdata
Hide JavaScript methods in a CDATA section for XHTML when "true".

defaultLocale

protected static Locale defaultLocale

Deprecated: This variable is no longer used.

The default locale on our server.

dynamicJavascript

protected String dynamicJavascript
The dynamic JavaScript objects will only be generated if this is set to "true".

formName

protected String formName
The name of the form that corresponds with the action name in struts-config.xml. Specifying a form name places a <script> </script> around the javascript.

htmlComment

protected String htmlComment
The JavaScript methods will enclosed with html comments if this is set to "true".

HTML_BEGIN_COMMENT

protected static final String HTML_BEGIN_COMMENT
The start of the HTML comment hiding JavaScript from old browsers.

Since: Struts 1.2

HTML_END_COMMENT

protected static final String HTML_END_COMMENT
The end of the HTML comment hiding JavaScript from old browsers.

Since: Struts 1.2

jsFormName

protected String jsFormName
formName is used for both Javascript and non-javascript validations. For the javascript validations, there is the possibility that we will be rewriting the formName (if it is a ValidatorActionForm instead of just a ValidatorForm) so we need another variable to hold the formName just for javascript usage.

lineEnd

protected static String lineEnd
The line ending string.

methodName

protected String methodName
This will be used as is for the JavaScript validation method name if it has a value. This is the method name of the main JavaScript method that the form calls to perform validations.

page

protected int page
The current page number of a multi-part form. Only valid when the formName attribute is set.

scriptLanguage

protected boolean scriptLanguage
Include language attribute in the <script> element. This property is ignored in XHTML mode.

Since: Struts 1.2

src

protected String src
The src attribute for html script element (used to include an external script resource). The src attribute is only recognized when the formName attribute is specified.

staticJavascript

protected String staticJavascript
The static JavaScript methods will only be printed if this is set to "true".

Method Detail

createActionList

private List createActionList(ValidatorResources resources, Form form)
Get List of actions for the given Form.

Parameters: resources form

Returns: A sorted List of ValidatorAction objects.

createDynamicJavascript

private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form)
Generates the dynamic JavaScript for the form.

Parameters: config resources locale form

createMethods

private String createMethods(List actions, boolean stopOnError)
Creates the JavaScript methods list from the given actions.

Parameters: actions A List of ValidatorAction objects. stopOnError If true, behaves like released version of struts 1.1 and stops after first error. If false, evaluates all validations.

Returns: JavaScript methods.

doStartTag

public int doStartTag()
Render the JavaScript for to perform validations based on the form name.

Throws: JspException if a JSP exception has occurred

escapeJavascript

private String escapeJavascript(String str)

Backslash-escapes the following characters from the input string: ", ', \, \r, \n.

This method escapes characters that will result in an invalid Javascript statement within the validator Javascript.

Parameters: str The string to escape.

Returns: The string s with each instance of a double quote, single quote, backslash, carriage-return, or line feed escaped with a leading backslash.

Since: Struts 1.2.8

escapeQuotes

private String escapeQuotes(String in)

getBundle

public String getBundle()
Sets the servlet context attribute key for our resources.

getCdata

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

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

getDynamicJavascript

public String 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.

getFormName

public 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.

getHtmlComment

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

getJavascriptBegin

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

getJavascriptEnd

protected String getJavascriptEnd()
Returns the closing script element.

getJavascriptStaticMethods

protected String getJavascriptStaticMethods(ValidatorResources resources)

getJsFormName

public String getJsFormName()

Returns: Returns the jsFormName.

getMethod

public 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.

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.

getScriptLanguage

public boolean getScriptLanguage()
Gets whether or not the <script> element will include the language attribute.

Returns: true if language attribute will be included.

Since: Struts 1.2

getSrc

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

getStaticJavascript

public String 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.

isXhtml

private boolean isXhtml()
Returns true if this is an xhtml page.

release

public void release()
Release any acquired resources.

renderJavascript

protected String renderJavascript()
Returns fully rendered JavaScript.

Since: Struts 1.2

renderStartElement

protected String renderStartElement()
Constructs the beginning <script> element depending on XHTML status.

Since: Struts 1.2

setBundle

public void setBundle(String bundle)
Gets the servlet context attribute key for our resources.

setCdata

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

Parameters: cdata The cdata to set

setDynamicJavascript

public void setDynamicJavascript(String 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.

setFormName

public void setFormName(String formName)
Sets 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. Specifying a form name places a <script> </script> tag around the javascript.

setHtmlComment

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

setJsFormName

public void setJsFormName(String jsFormName)

Parameters: jsFormName The jsFormName to set.

setMethod

public void setMethod(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.

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.

setScriptLanguage

public void setScriptLanguage(boolean scriptLanguage)
Sets whether or not the <script> element will include the language attribute.

Since: Struts 1.2

setSrc

public void setSrc(String src)
Sets the src attribute's value when defining the html script element. The src attribute is only recognized when the formName attribute is specified.

setStaticJavascript

public void setStaticJavascript(String 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.

stopOnError

private boolean stopOnError(ModuleConfig config)
Determines if validations should stop on an error.

Parameters: config The ModuleConfig used to lookup the stopOnError setting.

Returns: true if validations should stop on errors.

Copyright B) 2000-2007 - The Apache Software Foundation