@DefaultKey(value="validator") @ValidScope(value="request") public class ValidatorTool extends Object
View tool that works with Struts Validator to produce client side javascript validation for your forms.
Usage:
Template example: $validator.getJavascript("nameOfYourForm") Toolbox configuration: <tools> <toolbox scope="request"> <tool class="org.apache.velocity.tools.struts.ValidatorTool"/> </toolbox> </tools>
This is an adaptation of the JavascriptValidatorTag from the Struts 1.1 validator library.
Modifier and Type | Field and Description |
---|---|
protected javax.servlet.ServletContext |
app
A reference to the ServletContext
|
protected ViewContext |
context
A reference to the ViewContext
|
protected String |
jsFormName
formName is used for both Javascript and non-javascript validations.
|
protected javax.servlet.http.HttpServletRequest |
request
A reference to the HttpServletRequest.
|
protected org.apache.commons.validator.ValidatorResources |
resources
A reference to the ValidatorResources.
|
protected javax.servlet.http.HttpSession |
session
A reference to the HttpSession.
|
Constructor and Description |
---|
ValidatorTool() |
Modifier and Type | Method and Description |
---|---|
void |
configure(Map params)
Initializes this tool.
|
protected List |
createActionList(org.apache.commons.validator.ValidatorResources resources,
org.apache.commons.validator.Form form)
Get List of actions for the given Form.
|
protected String |
createMethods(List actions)
Creates the JavaScript methods list from the given actions.
|
protected String |
escapeJavascript(String str)
Backslash-escapes the following characters from the input string:
", ', \, \r, \n.
|
boolean |
getCdata()
Returns the cdata setting "true" or "false".
|
String |
getDynamicJavascript()
Render just the dynamic JavaScript to perform validations based
on the form name attribute of the action mapping associated
with the current request (if such exists).
|
String |
getDynamicJavascript(String formName)
Render just the dynamic JavaScript to perform validations based
on the supplied form name.
|
protected String |
getDynamicJavascript(org.apache.commons.validator.ValidatorResources resources,
Locale locale,
org.apache.commons.validator.Form form)
Generates the dynamic JavaScript for the form.
|
boolean |
getHtmlComment()
Gets whether or not to delimit the
JavaScript with html comments.
|
String |
getJavascript()
Render both dynamic and static JavaScript to perform
validations based on the form name attribute of the action
mapping associated with the current request (if such exists).
|
String |
getJavascript(String formName)
Render both dynamic and static JavaScript to perform
validations based on the supplied form name.
|
protected String |
getJavascript(String formName,
boolean getStatic)
Render both dynamic and static JavaScript to perform
validations based on the supplied form name.
|
protected String |
getJavascriptBegin(String methods)
Returns the opening script element and some initial javascript.
|
protected String |
getJavascriptEnd()
Returns the closing script element.
|
protected String |
getJavascriptStaticMethods(org.apache.commons.validator.ValidatorResources resources) |
String |
getMethod()
Gets the method name that will be used for the Javascript
validation method name if it has a value.
|
int |
getPage()
Gets the current page number of a multi-part form.
|
String |
getSrc()
Gets the src attribute's value when defining
the html script element.
|
String |
getStaticJavascript()
Render just the static JavaScript methods.
|
void |
init(Object obj)
Deprecated.
|
void |
setCdata(boolean cdata)
Sets the cdata status.
|
void |
setHtmlComment(boolean htmlComment)
Sets whether or not to delimit the
JavaScript with html comments.
|
void |
setMethod(String methodName)
Sets the method name that will be used for the Javascript
validation method name if it has a value.
|
void |
setPage(int page)
Sets the current page number of a multi-part form.
|
void |
setSrc(String src)
Sets the src attribute's value (used to include
an external script resource) when defining
the html script element.
|
protected ViewContext context
protected javax.servlet.ServletContext app
protected javax.servlet.http.HttpServletRequest request
protected javax.servlet.http.HttpSession session
protected org.apache.commons.validator.ValidatorResources resources
protected String jsFormName
@Deprecated public void init(Object obj)
public void configure(Map params)
params
- the Map of configuration parametersIllegalArgumentException
- if the param is not a ViewContextpublic int getPage()
public void setPage(int page)
page
- the current page number of a multi-part formpublic String getMethod()
public void setMethod(String methodName)
methodName
- the method name that will be used for the Javascript validation method namepublic boolean getHtmlComment()
public void setHtmlComment(boolean htmlComment)
htmlComment
- whether or not to delimit the JavaScript with html commentspublic String getSrc()
public void setSrc(String src)
src
- the src attribute's valuepublic boolean getCdata()
public void setCdata(boolean cdata)
cdata
- The cdata to setpublic String getJavascript() throws Exception
Exception
public String getJavascript(String formName) throws Exception
formName
- the key (form name)Exception
public String getDynamicJavascript() throws Exception
Exception
public String getStaticJavascript() throws Exception
Exception
public String getDynamicJavascript(String formName) throws Exception
formName
- the key (form name)Exception
protected String getJavascript(String formName, boolean getStatic) throws Exception
formName
- the key (form name)getStatic
- indicates if the static methods should be renderedException
protected String getDynamicJavascript(org.apache.commons.validator.ValidatorResources resources, Locale locale, org.apache.commons.validator.Form form)
resources
- the validator resourceslocale
- the locale for the current requestform
- the form to generate javascript forprotected 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.
str
- The string to escape.s
with each instance of a double quote,
single quote, backslash, carriage-return, or line feed escaped
with a leading backslash.protected String createMethods(List actions)
actions
- A List of ValidatorAction objects.protected List createActionList(org.apache.commons.validator.ValidatorResources resources, org.apache.commons.validator.Form form)
resources
- the validator resourcesform
- the form for which the actions are requestedprotected String getJavascriptBegin(String methods)
methods
- javascript validation methodsprotected String getJavascriptStaticMethods(org.apache.commons.validator.ValidatorResources resources)
resources
- the validation resourcesprotected String getJavascriptEnd()
Copyright © 2002–2021 Apache Software Foundation. All rights reserved.