JSPs can be created with the help of the Jargus Tag Library. If you are not familiar with JSP and Tag Libraries you should visit Sun JSP Site for further information.
This small example is a form which checks if three parameters are valid. The first parameter has to be non-null. The second parameter has to contain an @ as it could be an email address. And the last parameter has to be an integer. This are all fairly simple validations but as we will see you can load your customized Validator classes at runtime to perform your kind of validation.
As with every taglib, they have to be declared at the beginning of the jsp file. The prefix can be choosen freely.
<%-- ************************************************************ --%>
<%-- include taglib --%>
<%-- ************************************************************ --%>
<%@ taglib uri="http://www.caffeine.de/taglib" prefix="validation" %>
</head>
<body>
<h1>Test Form</h1>
According to HTML a form tag is used in the taglib for the same purpose. The action and method tags are used the same way. Either a handlerClassName or handlerbean attribute have to be set. If the first is used the tag loads and uses the class as its ExtendedValidationHandler. If the latter is given the tag tries to locate a bean with the given name in its session context and sets it as its ExtendedValidationHandler class. ValidatorClasspaths is a comma seperated list of package classpath where Validator classes are loaded from.
<%-- ************************************************************ --%>
<%-- validated html form --%>
<%-- ************************************************************ --%>
<validation:form method="POST" action="ok.html" handlerClassName="samples.servlet.HtmlFormValidationHandler"
validatorClasspaths="de.caffeine.jargus">
<table>
Print a summarized error message.
<font color="FF00FF">
<%-- ************************************************************ --%>
<%-- print validation message --%>
<%-- ************************************************************ --%>
<validation:message />
</font>
<tr>
With the check tag parameters can be set which should be validated. Validation argument refers to the Validator Class which will be used for the validation, e.g. if "exist" is given an ExistValidator will be loaded if it can be found.
<%-- ************************************************************ --%>
<%-- check whether parameter exists --%>
<%-- ************************************************************ --%>
<validation:check validation="exist">
The font tag is used to mark all parameters which failed with one color.
<%-- change label font color depending on result of valdation --%>
<td><validation:font name="p1">Parameter 1</validation:font></td>
Input tag is the corresponding tag to the html input tag.
<%-- validated text field --%>
<td><validation:input name="p1" type="text" /></td>
</validation:check>
</tr>
<tr>
<%-- ************************************************************ --%>
<%-- check if parameter contains given arguments --%>
<%-- ************************************************************ --%>
<validation:check validation="contains">
<%-- argument --%>
With argument tag an argument needed for a validation can be set. For example the @ has to be contained in the parameter value.
<validation:argument value="@"/>
<%-- change font color depending on result of validation --%>
<td><validation:font name="p2">Parameter 2</validation:font></td>
<%-- validated parameter --%>
<td><validation:input name="p2" type="text" /></td>
</validation:check>
</tr>
<tr>
<%-- ************************************************************ --%>
<%-- check if parameter is a integer value --%>
<%-- ************************************************************ --%>
<validation:check validation="integer">
<%-- change font color depending on result of validation --%>
<td><validation:font name="p3">Parameter 3</validation:font></td>
<%-- validated paramter --%>
<td><validation:input name="p3" type="text" /> </td>
</validation:check>
</tr>
<%-- sumbit button --%>
<tr><td><input type="submit" name="go" value="go" /></td></tr>
</table>
</validation:form>
</body>
</html>