Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Strut your stuff with JSP tags

Use and extend the open source Struts JSP tag library

  • Print
  • Feedback

Page 4 of 5

The implementation of the Validator interface simply stores the given patterns and messages in maps. As a bonus, it adopts the convention of interpreting patterns and messages starting with @ as references to predefined strings. Using predefined tags can help avoid typing errors on JSP pages and help build up a repository of tested regular expression patterns. An exception should be thrown if the predefined string isn't found, but I've left that out in the interest of brevity.

The validation itself predictably iterates over the fields (parameters) of the given request, locates any pattern that may have been associated with each field during the page construction (by a call to setPattern), and then calls match to compare the field value with the pattern. If the field value doesn't match the pattern, the validation method looks up the error message for the field and stores it in the errors map with the field name as key. From there, you can access it from the JSP by calling the getError(fieldName) method.

The implementation of the AutoBean interface is even simpler. The three functions directly access a member Map to set and get the pseudoproperties.

Listing 6. PageBean.java: The base class for page beans

package com.bank;
import java.util.*;
import org.apache.oro.text.perl.*;
public class PageBean implements Validator, AutoBean {
    // implementing Validator
    private Perl5Util perl = new Perl5Util();
    private Map patterns = new Hashtable();
    private Map messages = new Hashtable();
    private Map errors = new Hashtable();
    private static Map namedPatterns = new Hashtable();
    private static Map namedMessages = new Hashtable();
    public void setPattern(String field, String pattern) {
        if(pattern == null  || pattern.equals(""))
            return;
        if(pattern.charAt(0) == '@')
            pattern = (String) namedPatterns.get(pattern);
        patterns.put(field, pattern);
    }
    public void setErrorMessage(String field, String message) {
        if(message == null || message.equals(""))
            return;
        if(message.charAt(0) == '@')
            message = (String) namedMessages.get(message);
        messages.put(field, message);
    
    public boolean validate(javax.servlet.http.HttpServletRequest request) {
        errors.clear();
        Enumeration e = request.getParameterNames();
        while(e.hasMoreElements()) {
            String field = (String) e.nextElement();
            validateField(field, request.getParameter(field));
        }
        return errors.isEmpty();
    }
    private boolean validateField(String field, String value) {
        String pattern = (String) patterns.get(field);
        if(pattern == null)
            return true;
        if(perl.match(pattern, value))
            return true;
        errors.put(field, messages.get(field));
        return false;
    }
    public String getError(String name) {
        String result = (String) errors.get(name);
        if(result == null)
            return "";
        return result;
    }
    static {
        // Predefined patterns and messages
        namedPatterns.put("@amount", "/^[1-9][0-9]{0,8}(,[0-9]{1,2})?$/");
        namedPatterns.put("@account", "/^[0-9]{7,9}$/");
        namedPatterns.put("@personName", "/^[a-zA-Z ]{0,30}$/");
        namedMessages.put("@amount", "Amount can have 8 digits plus 2 decimals");
        namedMessages.put("@account", "Account must have between 7 and 9 digits");
        namedMessages.put("@personName", "Name can have up to 30 alphabetic characters");
    }
    // implementing AutoBean
    private Map properties = new Hashtable();
    public void set(String property, Object value) {
        properties.put(property, value);
    }
    public String get(String property) {
        return properties.get(property).toString();
    }
    public Object getObject(String property) {
        return properties.get(property);
    }
}


One small task remains to be done. You must change the tag library definition file, struts.tld, to reflect the new attributes. Listing 7 shows how you insert the new attributes into the text tag. If you want to use that with other tags, such as the password tag or the text area tags, then you must also change them.

  • Print
  • Feedback

Resources