If you implemented a Validator interface rather than implementing a backing bean method that performs the validation, you need to do one of the following:
Allow the page author to specify the Validator implementation to use with the validator tag. In this case, the Validator implementation must define its own properties. Using a Custom Validator explains how to use the validator tag.
Create a custom tag that provides attributes for configuring the properties of the validator from the page. Because the Validator implementation from the preceding section does not define its attributes, the application developer must create a custom tag so that the page author can define the format patterns in the tag.
To create a custom tag, you need to do two things:
Write a tag handler to create and register the Validator implementation on the component.
Write a TLD to define the tag and its attributes.
Using a Custom Validator explains how to use the custom validator tag on the page.
The tag handler associated with a custom validator tag must extend the ValidatorELTag class. This class is the base class for all custom tag handlers that create Validator instances and register them on UI components. The FormatValidatorTag class registers the FormatValidator instance onto the component.
The FormatValidatorTag tag handler class does the following:
Sets the ID of the validator.
Provides a set of accessor methods for each attribute defined on the tag.
Implements the createValidator method of the ValidatorELTag class. This method creates an instance of the validator and sets the range of values accepted by the validator.
The formatPatterns attribute of the formatValidator tag supports literals and value expressions. Therefore, the accessor method for this attribute in the FormatValidatorTag class must accept and return an instance of ValueExpression:
protected ValueExpression formatPatterns = null; public void setFormatPatterns(ValueExpression fmtPatterns){ formatPatterns = fmtPatterns; }
Finally, the createValidator method creates an instance of FormatValidator, extracts the value from the formatPatterns attribute’s value expression and sets the formatPatterns property of FormatValidator to this value:
the formatPatterns property of FormatValidator to this value:
protected Validator createValidator() throws JspException { FacesContext facesContext = FacesContext.getCurrentInstance(); FormatValidator result = null; if(validatorID != null){ result = (FormatValidator) facesContext.getApplication() .createValidator(validatorID); } String patterns = null; if (formatPatterns != null) { if (!formatPatterns.isLiteralText()) { patterns = (String) formatPatterns.getValue(facesContext.getELContext()); } else { patterns = formatPatterns.getExpressionString(); }
To define a tag, you declare it in a tag library descriptor (TLD), which is an XML document that describes a tag library. A TLD contains information about a library and each tag contained in it. See Tag Library Descriptors for more information about TLDs.
The custom validator tag is defined in bookstore.tld, located in the tut-install/javaeetutorial5/examples/web/bookstore6/web/WEB-INF/ directory. It contains a tag definition for formatValidator:
<tag> <name>formatValidator</name> ... <tag-class> com.sun.bookstore6.taglib.FormatValidatorTag</tag-class> <attribute> <name>formatPatterns</name> <required>true</required> <deferred-value> <type>String</type> </deferred-value> </attribute> </tag>
The name element defines the name of the tag as it must be used in the page. The tag-class element defines the tag handler class. The attribute elements define each of the tag’s attributes. The formatPatterns attribute is the only attribute that the tag supports. The deferred-value element indicates that the formatPatterns attribute accepts deferred value expressions. The type element says that the expression resolves to a property of type String.