VelocityStruts
Other Subprojects
This tool deals with Struts error messages. Errors may stem from the validation of a submitted form or from the processing of a request. If there are errors, they are made available to the view to render. A few important aspects about errors are:
property that describes the category of
error. This allows the view designer to place error messages precisely where an
error occurred. For example, errors that apply to the entire page can be rendered
at the top of the page, errors that apply to a specific input field can be rendered
next to this input field. Several methods of this tool provide a parameter
property that allows to select a specific category of errors to operate
on. Without the property parameter, methods operate on all error messages.See the Struts User's Guide, section Building View Components for more information on this topic.
<tool> <key>errors</key> <scope>request</scope> <class>org.apache.velocity.tools.struts.ErrorsTool</class> </tool>
$errors.get($errors.globalName)
true if there are errors queued, otherwise false.
boolean exist()
boolean exist(String property)
true if there are error messages queued. false otherwise.
Calling exist() without the property parameter checks for error messages of
any category. The property parameter can be used to limit the check to
error messages of a specific category.
$errors.exist() $errors.exist("password")
int getSize()
int getSize(String property)
Calling getSize() without the property parameter returns the total
number of queued error messages. The property parameter can be used to
obtain the number of queued error messages for a specific category.
$errors.getSize() $errors.size $errors.getSize("password")
$errors.get($errors.globalName)
List getGlobal()
List getAll()
List getAll(String bundle)
The following example shows a macro to render the error messages:
#macro (errorMarkup) #if ($errors.exist()) <ul> #foreach ($e in $errors.all ) <li>$e</li> #end </ul> #end #end
This produces output similar to the following:
<ul> <li>The field Expiration Date is required.</li> <li>The provided number is not a valid credit card number</li> </ul>
List get(String property)
List get(String property, String bundle)
java.util.List of java.lang.String.
If no error messages exist for the specified category,
null is returned.
If the message resources are lacking an error message for a
particular message key, the key itself is used as an error message
and a warning is logged.
The following example shows a macro to render the error messages for a particular category of errors:
#macro (errorMarkup $property) #if ($errors.exist($property)) <ul> #foreach ($er in $errors.get($property) ) <li>$er</li> #end </ul> #end #end
This produces output similar to the following:
<ul> <li>The field Expiration Date is required.</li> <li>The provided number is not a valid credit card number</li> </ul>
String getMsgs()
String getMsgs(String property)
String getMsgs(String property, String bundle)
String is returned.
This method renders the queued error messages as a list. If the method
is called without a parameter, all queued errors are rendered. With the
parameter property the list of rendered messages can be
limited to a specific category of errors. Error message texts
are looked up in the message resources. If a message text
cannot be found, the message key will be displayed instead.
The method expects a message header, a message footer, a message prefix
and a message suffix to be defined in the message resources. The corresponding
message keys are errors.header, errors.footer,
errors.prefix and errors.suffix.
Assuming that the message resources contain the following definitions:
errors.header=Please correct the following errors before proceeding:<ul> errors.footer=</ul> errors.prefix=<li> errors.suffix=</li> error01=The field Expiration Date is required. error02=The input is not a valid credit card number. ...
an error message would be rendered as follows:
Please correct the following errors before proceeding:<ul> <li>The field Expiration Date is required.</li> <li>The input is not a valid credit card number.</li> </ul>