bea.com | products | dev2dev | support | askBEA |
|
e-docs > WebLogic Server > Programming WebLogic JSP > Using WebLogic JSP Form Validation Tags |
Programming WebLogic JSP |
Using WebLogic JSP Form Validation Tags
The following sections describe how to use WebLogic JSP form validation tags:
Overview of WebLogic JSP Form Validation Tags
WebLogic JSP form validation tags provide a convenient way to validate the entries an end user makes to HTML form text fields generated by JSP pages. Using the WebLogic JSP form validation tags prevents unnecessary and repetitive coding of commonly used validation logic. The validation is performed by several custom JSP tags that are included with the WebLogic Server distribution. The tags can
WebLogic JSP form validation tags include:
When a validation tag determines that data in a field is not been input correctly, the page is re-displayed and the fields that need to be re-entered are flagged with text or an image to alert the end user. Once the form is correctly filled out, the end user's browser displays a new page specified by the validation tag.
Validation Tag Attribute Reference
This section describes the WebLogic form validation tags and their attributes. Note that the prefix used to reference the tag can be defined in the taglib directive on your JSP page. For clarity, the wl prefix is used to refer to the WebLogic form validation tags throughout this document.
<wl:summary> is the parent tag for validation. Place the opening <wl:summary> tag before any other element or HTML code in the JSP. Place the closing </wl:summary> tag anywhere after the closing </wl:form> tag(s).
for (int i=0; i < errorVector.size(); i++) {
out.println((String)errorVector.elementAt(i));
out.println("<br>");
}
} %>
out.println(headerText);
}
%>
The <wl:form> tag is similar to the HTML <form> tag and defines an HTML form that can be validated using the the WebLogic JSP form validataion tags. You can define multiple forms on a single JSP by uniquely identifying each form using the name attribute.
Use one or more <wl:validator> tags for each form field. If, for instance, you want to validate the input against a regular expression and also require that something be entered into the field you would use two <wl:validator> tags, one using the RequiredFieldValidator class and another using the RegExpValidator class. (You need to use both of these validators because blank values are evaluated by the Regular Expression Field Validator as valid.)
Using WebLogic JSP Form Validation Tags in a JSP
To use a validation tag in a JSP:
<%@ taglib uri="tagl" prefix="wl" %>
Note that the prefix attribute defines the prefix used to reference all tags in your JSP page. Although you may set the prefix to any value you like, the tags referred to in this document using the wl prefix.
Place the opening <wl:summary ...> tag before any HTML code, JSP tag, scriptlet, or expression on the page.
Place the closing </wl:summary> tag anywhere after the </wl:form> tag(s).
The following example shows a validation for a required field:
<wl:form name="FirstForm" method="POST" action="thisJSP.jsp">
<wl:validator
errorMessage="Field_1 is required" expression=""
fieldToValidate="field_1"
validatorClass=
"weblogicx.jsp.tags.validators.RequiredFieldValidator"
>
<img src="images/warning.gif">
<font color=red>Field 1 is a required field</font>
</wl:validator>
<p> <input type="text" name = "field_1"> </p>
<p> <input type="text" name = "field_2"> </p>
<p> <input type="submit" value="Submit FirstForm"> </p>
</wl:form>
If the user fails to enter a value in field_1, the page is redisplayed, showing a warning.gif image, followed by the text (in red) "Field 1 is a required field," followed by the blank field for the user to re-enter the value.
<taglib>
<taglib-uri>tagl</taglib-uri>
<taglib-location>
/WEB-INF/lib/weblogic-vtags.jar
</taglib-location>
</taglib>
For more information on Web Application deployment descriptors, see Writing Web Application Deployment Descriptors.
Creating HTML Forms Using the <wl:form> Tag
This section contains information on creating HTML forms in your JSP page. You use the <wl:form> tag to create a single form or multiple forms on a page.
Use the <wl:form> tag that is provided in the weblogic-vtags.jar tag library: For example:
<wl:form method="POST" action="nextPage.jsp">
<p> <input type="text" name ="field_1"> </p>
<p> <input type="text" name ="field_2"> </p>
<p> <input type="submit" value="Submit Form"> </p>
</wl:form>
For information on the syntax of this tag see <wl:form>" on page 3.
When using multiple forms on a page, use the name attribute to identify each form. For example:
<wl:form name="FirstForm" method="POST" action="thisJSP.jsp">
<p> <input type="text" name="field_1"> </p>
<p> <input type="text" name="field_2"> </p>
<p> <input type="submit" value="Submit FirstForm"> </p>
</wl:form>
<wl:form name="SecondForm" method="POST" action="thisJSP.jsp">
<p> <input type="text" name="field_1"> </p>
<p> <input type="text" name="field_2"> </p>
<p> <input type="submit" value="Submit SecondForm"> </p>
</wl:form>
Re-Displaying the Values in a Field When Validation Returns Errors
When the JSP page is re-displayed after the validator tag has found errors, it is useful to re-display the values that the user already entered, so that the user does not have to fill out the entire form again. Use the value attribute of the HTML <input> tag or use a tag library available from the Apache Jakarta Project. Both procedures are described next.
Re-Displaying a Value Using the <input> Tag
You can use the javax.servlet.ServletRequest.getParameter() method together with the value attribute of the HTML <input> tag to re-display the user's input when the page is re-displayed as a result of failed validation. For example:
<input type="text" name="field_1"
value="<%= request.getParameter("field_1") %>" >
Re-Displaying a Value Using the Apache Jakarta <input:text> Tag
You can also use a JSP tag library available free from the Apache Jakarta Project, which provides the <input:text> tag as a replacement for the HTML <input> tag. For example, the following HTML tag:
<input type="text" name="field_1">
could be entered using the Apache tag library as:
<input:text name="field_1">
For more information and documentation, download the Input Tag library.
To use the Apache tag library in your JSP:
<%@ taglib uri="input" prefix="input" %>
<taglib>
<taglib-uri>input</taglib-uri>
<taglib-location>/WEB-INF/lib/input.jar</taglib-location>
</taglib>
Using a Custom Validator Class
To use your own validator class:
<wl:validator errorMessage="This field is required" fieldToValidate="field_1" validatorClass="mypackage.myCustomValidator">
Extending the CustomizableAdapter Class
The CustomizableAdapter class is an abstract class that implements the Customizable interface and provides the following helper methods:
Instead of extending the CustomizableAdapter class, you can implement the Customizable interface. For more information, see the Javadocs for weblogicx.jsp.tags.validators.Customizable.
Sample User-Written Validator Class
Listing 5-1 Example of a User-written Validator Class
import weblogicx.jsp.tags.validators.CustomizableAdapter;
public class myCustomValidator extends CustomizableAdapter{
public myCustomValidator(){
super();
}
public boolean validate(javax.servlet.ServletRequest req)
throws Exception {
String val = req.getParameter(getFieldToValidate());
// perform some validation logic
// if the validation is successful, return true,
// otherwise return false
if (true) {
return true;
}
return false;
}
}
Sample JSP with Validator Tags
This sample code shows the basic structure of a JSP that uses the WebLogic JSP form validation tags. A complete functioning code example is also available if you installed the examples with your WebLogic Server installation. Instructions for running the example are available at samples/examples/jsp/tagext/form_validation/package.html, in your WebLogic Server installation.
Listing 5-2 JSP with WebLogic JSP Form Validation Tags
<%@ taglib uri="tagl" prefix="wl" %>
<%@ taglib uri="input" prefix="input" %>
<wl:summary
name="summary "
headerText="<font color=red>Some fields have not been filled out correctly.</font>"
redirectPage="successPage.jsp"
>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<% if(summary.size() >0 ) {
out.println("<h3>" + headerText + "</h3>");
} %>
<% if (summary.size() > 0) {
out.println("<H2>Error Summary:</h2>");
for (int i=0; i < summary.size(); i++) {
out.println((String)summary.elementAt(i));
out.println("<br>");
}
} %>
<wl:form method="GET" action="successPage.jsp">
User Name: <input:text name="username"/>
<wl:validator
fieldToValidate="username"
validatorClass="weblogicx.jsp.tags.validators.RequiredFieldValidator"
errorMessage="User name is a required field!"
>
<img src=images/warning.gif> This is a required field!
</wl:validator>
<p>
Password: <input type="password" name="password">
<wl:validator
fieldToValidate="password"
validatorClass="weblogicx.jsp.tags.validators.RequiredFieldValidator"
errorMessage="Password is a required field!"
>
<img src=images/warning.gif> This is a required field!
</wl:validator>
<p>
Re-enter Password: <input type="password" name="password2">
<wl:validator
fieldToValidate="password,password2"
validatorClass="weblogicx.jsp.tags.validators.CompareValidator"
errorMessage="Passwords don't match"
>
<img src=images/warning.gif> Passwords don't match.
</wl:validator>
<p>
<input type="submit" value="Submit Form"> </p>
</wl:form>
</wl:summary>
</body>
</html>