Downloads:
html2.zip- the jar file and the Validator rules xml file.
html2-src.zip - all source files.
html2demo.war - the demo file.
All files are distributed under the Apache 2.0 License.
The html2 tag library provides an alternate way of handling invalid (and valid) form input.
Combined with some CSS style definitions, your form fields can be declared like this:
<html:field property="salary">
<html:label/>
<html:text/>
<html:error styleClass="myCssStyleClass"/>
</html:field>
... and invalid fields will be shown like these:
There are two modes provided, one for forms not using the Validator support, and another for those using Validator.
Without Validator's client-side validation, <html2> tags will call
the ActionForm's validate() method. This is done through
an XMLHttpRequest to an action mapping created by the Html2Plugin.
To enable this:
Include html2.jar in your web application.
<plug-in className="net.rabago.struts.html2.Html2PlugIn"/>
Use <html2> tags for form fields.
(See below)
html2Validate() javascript method when the
form is submitted:
<html2:form action="/submitForm" onsubmit="return html2Validate(this)">
Include the <html2:html2Javascript/>
tag in the page.
The javascript code in the current Validator plugin hardcodes its handling of validation results. This prevents applications (or add-ons like this one) from customizing its behavior. To solve this, we use the validatorjs extension by Niall Pemberton (proposed as a commons-validator enhancement here).
To use this:
Include html2.jar and validatorjs_struts.jar in your web application.
Use <html2> tags for form fields.
(See below)
validate[FormName]() javascript method
when the form is submitted:
<html2:form action="/submitForm" onsubmit="return validateEmployeeForm(this)">
... the way you do now with Validator-supported forms, or use the new
validatorForm attribute provided by the
validatorjs extension:
<html2:form action="/submit" validatorForm="employeeForm">
Include the <html2:html2Javascript/>
tag in the page. If all forms on this page are Validator-supported,
you can turn html2 XMLHttpRequest support off:
<html2:html2Javascript xmlHttpRequest="false"/>
Since Validator provides client-side validation, I was able to save the generated
HTML code and provide it here:
validatorDemo.htm - Struts Validator + ValidatorJS extension + html2
customHandlingDemo.htm - same as above, plus custom field handling.
The use of <html2> tags for form fields are needed for html2 functionality. There are two sets of tags that are provided, one for use without Validator, the other for use with Validator:
<%@ taglib uri="http://www.rabago.net/tags-html2" prefix="html2" %> <%@ taglib uri="http://www.rabago.net/tags-valid2" prefix="html2" %>As you might guess, the
tags-html2 library is for use on forms
not using Validator, and the tags-valid2 library is for those
using Validator.
Each set contains about 10 custom implementations of existing Struts tags,
plus about 4 new tags (field, label,
error, and html2Javascript).
You'll only need to use one set in a given JSP, of course. You can continue to use the standard HTML tag library for those not customized by <html2>, but for convenience, those same tags were declared in these TLD files so you can stick to just one set of HTML tags.
The demo application shows examples of mixing
<html2> tags with the standard Struts HTML tags in both
actionFormDemo.jsp and validatorDemo.jsp. In these
files you'll see a mix of <html2> tags and the usual Struts <html>
tags.
The customHandlingDemo.jsp file shows an example of using the
<html2> TLD for both <html2> and Struts <html> tags.
The Javascript generated by the <html2> tags use the ID attribute of HTML tags to find the labels and error tags associated with valid and invalid fields. For this reason, forms that need to specify their own ID value may not be able to use <html2> tags.
The ID values used are:
| Element | ID format |
|---|---|
| Form | [beanName] |
| Input control | [beanName].[fieldName] |
| Input label | [beanName].[fieldName].label |
| Error display <span> tag | [beanName].[fieldName].error |
If I find the time, that is. Or someone else volunteers and picks this up.